Apologies if this isn't the right place to ask this. I occasionally get alerts from my Django website along these lines:
Internal Server Error: /posts/specific-post/
UnboundLocalError at /posts/specific-post/ local variable 'comment_form' referenced before assignment
HEAD requests are triggering these errors. This is probably because my view code is structured like this:
if request.method == "POST":
comment_form = CommentForm(request.POST)
# handle the form ...
# finally,
return HttpResponseRedirect('/comment/')
else if request.method == "GET":
comment_form = CommentForm()
context = {
"comment_form": comment_form,
}
return render(request, 'detail.html', context)
The Django docs tend to use a simple if else
, rather than if else if
:
if request.method == "POST":
# ...
else:
# ...
So my question is this: Is there any reason why I shouldn't use the simple if else
, and allow not only POST and GET requests, but all other types of requests (including ostensibly HEAD requests)? And more out of curiosity, are the HEAD requests from search engine indexers or something? Thanks for any advice.