Py学习  »  Django

未加载django表单初始值

Max • 4 年前 • 645 次点击  

我不明白为什么django 1.8没有选择初始值。 这是我的代码:

class TestForm(forms.Form):
    x = forms.ChoiceField(choices=[('a', 'a'), ('b', 'b')], initial=('b', 'b'), required=True)


@staff_member_required
def view(request):
    data = Bunch()
    if request.method == "GET":
        if 'save' in request.GET:
            data.form = TestForm(request.GET)
            if data.form.is_valid():
                pass
            else:
                logger.error('Invalid form.')
        else: # nothing is submitted:
            data.form = TestForm(initial={'x': ('b', 'b')})


        return render(request, 'test.html', data)
    return HttpResponse("Invalid request method: %s" % request.method)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39873
 
645 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Max
Reply   •   1 楼
Max    5 年前

我的错误是初始值应该是id,而应该是id和value的元组。