Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango Form not rendering
    text
    copied!<p>I have the following form in forms.py: </p> <pre><code>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 </code></pre> <p>And in my view the method looks like this: </p> <pre><code>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}) </code></pre> <p>But the form won't render in the browser. All that shows up is the submit button. My HTML looks like this: </p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; {% if contractForm.errors %} &lt;p style="color: red;"&gt; Please correct the error{{ contractForm.errors|pluralize }} below. &lt;/p&gt; {% endif %} &lt;form method="POST" action=""&gt; &lt;table&gt; {{ contractForm.as_table }} &lt;/table&gt; &lt;input type="submit" value="Submit" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>So why won't the form render?</p> <hr> <p><strong>EDIT</strong> I have got rid of the custom client field requiring the user and it still won't render. I thought this might help. So a form could use this class: </p> <pre><code>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()) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload