Reputation: 257
I'm trying to display a form on a template, but I get a fantastic error :
Caught AttributeError while rendering: 'WSGIRequest' object has no attribute 'get'
The error is in this line : {% for field in form.visible_fields %}
My view :
def view_discussion(request, discussion_id):
discussion = get_object_or_404(Discussion, id=discussion_id)
form = BaseMessageForm(request)
return render(request,'ulule/discussions/view_discussion.html', {
'discussion':discussion,
'form':form,
})
My form :
class BaseMessageForm(forms.Form):
message_content = forms.CharField(widget=forms.HiddenInput())
My template :
<form action="" method="post">
{% csrf_token %}
{% for field in form.visible_fields %}
<div class="fieldWrapper">
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Send message" /></p>
</form>
Thanks a lot for your help !
Upvotes: 3
Views: 2061
Reputation: 10086
If I remember correctly, the error you are getting happens because you got the signature of form's initializer wrong: the first argument to it is "data", which in your case resides in request.POST (and not the request itself), if you are arriving on a POST that is.
Commonly a view with a form will look something like this:
def my_view(request, ...):
if request.method == 'POST': # The form has been submitted
form = MyForm(request.POST)
if form.is_valid():
# do whatever you want here, save the form, etc
else:
form = MyForm()
return render_to_response('myform.html', ... )
Upvotes: 6