Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6475089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:39:50+00:00 2026-05-25T06:39:50+00:00

I’m trying to create a comments section for a web page. However, the function

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:39:51+00:00Added an answer on May 25, 2026 at 6:39 am

    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:

    def comment(request):
        commentlist = []
        comment = ''
    

    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 commentlist variable – try setting it in the else statement, just to see if you’re handling the GET correctly:

        else: #form invalid
            commentlist = ['bad form']
    else:
        form = CommentForm()
        commentlist = ['testing']
    

    Update – wups, I just noticed you had both comment and commentlist in the template dictionary. Take out comment:

    initialData = ({'form': form, 'commentlist': commentlist}
    

    Otherwise I suspect that it’s interfering with the comment variable in the template.

    Update 2 – sigh. I didn’t read the code closely enough – you have an HttpResponseRedirect right after assigning commentlist – so the list is never set and passed to render_to_response. If you’re thinking that commentlist will 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 the render_to_response line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.