Very confused about this one. This code in views.py works, but only when I’m debugging using Pycharm. If I just do runserver I get a 500 error.
views.py:
def add_post(request):
if request.method == 'POST':
form = PostForm(request.POST)
cd = form.cleaned_data
if form.is_valid():
print "valid"
post = Post(nickname=cd['nickname'], body=cd['body'], category=cd['category'])
post.save()
return HttpResponse("success")
return HttpResponseServerError("fail")
Error as seen in Chrome Inspector
<th>Exception Value:</th>
<td><pre>'PostForm' object has no attribute 'cleaned_data'</pre></td>
No attribute cleaned_data? But why…?
The
cleaned_dataattribute becomes available after callingis_valid()on a form. You should movecd = form.cleaned_datato below theif.