【问题标题】:Django Form not renderingDjango表单不呈现
【发布时间】:2011-02-14 00:12:10
【问题描述】:

我在forms.py中有如下表格:

class ContractForm(forms.Form):
    title = forms.CharField()
    start_date = forms.DateField()
    end_date = forms.DateField()
    description = forms.CharField(widget=forms.Textarea)
    client = forms.ModelChoiceField(queryset=Client.objects.all())

    def __init__(self, user, *args, **kwargs):
        super(ContractForm, self).__init__(*args, **kwargs)
        self.fields['client'] = forms.ModelChoiceField(queryset=Client.objects.filter(user=user))
        clients = Client.objects.filter(user = user)
        for client in clients:
            print client   

在我看来,该方法如下所示:

def addContract(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/contractManagement/login/?next=%s' % request.path)
else:   
    if request.method == 'POST':
        contractForm = ContractForm(request.POST)
        title = request.POST['title']
        start_date = request.POST['start_date']
        end_date = request.POST['end_date']
        description = request.POST['description']
        client = request.POST['client']
        user = request.user
        contract = Contract(title,start_date,end_date,description,client,user)
        contract.save()
        return HttpResponseRedirect('../../accounts/profile/')
    else:
        user = request.user
        print user.username
        contractForm = ContractForm(user)
        return render_to_response('newcontract.html', {'contractForm': contractForm})

但表单不会在浏览器中呈现。显示的只是提交按钮。我的 HTML 看起来像这样:

<html>
<head>
</head>
<body>
{% if contractForm.errors %}
<p style="color: red;">
    Please correct the error{{ contractForm.errors|pluralize }} below.
</p>
{% endif %}
<form method="POST" action="">    
    <table>
        {{ contractForm.as_table }}
    </table> 
    <input type="submit" value="Submit" />
</form> 
 </body>
</html>

那么为什么表单不渲染呢?


编辑 我已经摆脱了需要用户的自定义客户端字段,但它仍然不会呈现。我认为这可能会有所帮助。 所以一个表单可以使用这个类:

class ContractForm(forms.Form):
    title = forms.CharField()
    start_date = forms.DateField()
    end_date = forms.DateField()
    description = forms.CharField(widget=forms.Textarea)
    client = forms.ModelChoiceField(queryset=Client.objects.all()) 

【问题讨论】:

  • 当您在页面上执行 GET 时,表单是否正常工作?另外,不要一开始就进行身份验证检查,而是签出 login_required 装饰器,它会为你做这件事。
  • @sdolan 不,它对它没有任何影响。

标签: django django-forms


【解决方案1】:

forms.ModelChoiceFieldqueryset 参数应该是一个查询集。它应该是:

self.fields['client'] = forms.ModelChoiceField(queryset=Client.objects.filter(user=user))

而且您必须先在表单中声明它,然后在 __init__() 方法中覆盖它:

class ContractForm(forms.Form):
    ...
    client = forms.ModelChoiceField(queryset=Client.objects.all())

【讨论】:

  • 不管怎样,Amaud 是对的——查询集必须是实际的 QuerySet 对象。由于您的问题一直存在,您能否更新您的代码示例以反映您的 ContactForm 类的当前状态?
【解决方案2】:

除了 Arnaud 指出的 QuerySet 问题之外,这里还有很多问题。

首先,当您实例化表单以响应 GET 时,您执行contractForm = ContractForm(user)。但是表单实例化的第一个参数(正如您在 POST 中所做的那样)是发布的数据 - 您没有更改表单的 __init__ 签名。

其次,在 __init__ 本身中,您引用了 user 变量:但实际上您并没有从任何地方获得该变量。如果不从某个地方接收变量,您就无法神奇地引用它们。

因此,要修复这些错误,您需要按如下方式更改 init 方法:

def __init__(self, *args, **kwargs):
    user = kwargs.pop('user')
    super(ContractForm, self).__init__(*args, **kwargs)
    self.fields['client'].queryset=Client.objects.filter(user=user)

并像这样在视图中实例化表单:

contractForm = ContractForm(user=request.user)

第三,虽然这与您发布的问题无关,但您永远不会在此表单上看到任何错误,因为您没有在 POST 部分检查它是否有效 - 您需要检查 if contractForm.is_valid(),如果不会落到最后的render_to_response(需要取消缩进一级)。

此外,通过直接从request.POST 设置模型,您忽略了拥有表单的全部意义。您应该使用 contractForm.cleaned_data 的内容来创建 Contract 实例。

最后,您应该调查一下 ModelForm 是否能更好地满足您的需求。

【讨论】:

  • 它仍然没有呈现表单。
【解决方案3】:

更改视图中表单的名称。现在是这个样子

form = ContractForm(user=request.user)

而且它有效。

【讨论】:

    猜你喜欢
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 2019-04-25
    • 2015-08-13
    相关资源
    最近更新 更多