I’m trying to create a comments section for a web page. However, the function I used in my views.py file returns an error: “Local variable referenced before assignment.” Here is the code:
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django import forms
from django.db import models
from django.template import RequestContext
class CommentForm(forms.Form):
name = forms.CharField(max_length=100)
email = forms.EmailField(required=False)
comment = forms.CharField(widget=forms.Textarea)
def clean_message(self):
message = self.cleaned_data['message']
num_words = len(message.split())
return message
def comment(request):
commentlist = []
errors = []
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.cleaned_data
commentlist.append(comment)
return HttpResponseRedirect('')
else:
form = CommentForm()
initialData = ({'form': form, 'commentlist': commentlist, 'comment': comment})
csrfContext = RequestContext(request, initialData)
return render_to_response('hello_world.htm', csrfContext)
Here is my template, called “hello_world.htm”:
<html>
<head>
<style>
ul.errorlist{
margin: 0;
padding: 0;
}
.errorlist li{
background-color: red;
color: white;
display: block;
font-size: 10px;
margin: 0 0 3px;
padding: 4px 5px;
}
.field
{
background-color:#E2F2F2;
border: white 1px solid;
padding: 5px;
width: 80%;
font-family: Arial;
}
.field2
{
background-color:#E2F2E2;
border: white 1px solid;
padding: 5px;
width: 80%;
font-family: Arial;
}
.header
{
background-color:#E2E2F2;
border: white 1px solid;
padding: 5px;
width: 80%;
font-family: Arial;
}
</style>
</head>
<body>
Hello world
<br/>
<br/>
<br/>
<br/>
<div class="field2">
Comments:
</div>
{% for comment in commentlist %}
<div class="field2">
{{ comment }}
</div>
{% endfor %}
{% if form.errors %}
<p style="color: red;"><b>Please correct the error{{ form.errors|pluralize }} below. </b></p>
{% endif %}
<div class="field"><b>Want to comment?</b></div>
<form action="" method="post">
{% csrf_token %}
<div class="field">
{{ form.name.errors }}
<label for="id_subject">Name:</label>
{{ form.name }}
</div>
<div class="field">
{{ form.email.errors }}
<label for="id_email">Your e-mail address</label>
{{ form.email }}
</div>
<div class="field">
{{ form.comment.errors }}
<p>Comment:</p>
{{ form.comment }}
<br/>
<input type="submit" value="Submit">
</div>
How can I rewrite my views.py (particularly the “comments” function) so that this error will not come up? Sorry if there are many errors in my code, I have only been working with Django for a few days. Thanks!
I see one variable that might not be assigned, depending on how the if statements fire:
comment. Set it to null or something to start:And rename the function to something else, maybe
get_comment()– it’s a bad idea to have variables the same name as functions.Also, for debugging the
commentlistvariable – try setting it in the else statement, just to see if you’re handling theGETcorrectly:Update – wups, I just noticed you had both
commentandcommentlistin the template dictionary. Take out comment:Otherwise I suspect that it’s interfering with the
commentvariable in the template.Update 2 – sigh. I didn’t read the code closely enough – you have an
HttpResponseRedirectright after assigningcommentlist– so the list is never set and passed torender_to_response. If you’re thinking thatcommentlistwill be built up over several calls – it won’t, because you’re setting it to an empty list at the start of the function. Basically, you need to re-write all of the logic. 🙂Or you could try commenting out the
HttpResponseRedirect, and let it fall through to therender_to_responseline.