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

  • SEARCH
  • Home
  • 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 1927932
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:55:09+00:00 2026-05-17T06:55:09+00:00

I am having problem in django. I have created a form in my app

  • 0

I am having problem in django. I have created a form in my app where I can take details of a client. Now I want to create a form which can allow me to edit a form. However I am having some problems when I go to /index/edit_client/1, I get this error.

local variable 'form' referenced before assignment

I do not know what the reason why I have got this error, but from what I have looked at, it does not help matters unless of course there is another way how to create an edit form to edit the clients form. Here are some output that can be helpful too.

# urls.py
    urlpatterns = patterns('',
    (r'^index/$', login_required(direct_to_template), { 'template': 'index.html' }),
    (r'^index/clients/$', client_info),
    (r'^index/clients_details/(?P<id>\d+)/$', clients_details),
    (r'^index/edit_client/(?P<id>\d+)/$', edit_client),
)

# views.py
@login_required 
def edit_client(request, id=1):
    clients_list = Client.objects.filter(pk=id)  
    if request.method == 'POST':
        form = ClientForm(request.POST or None)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/index/clients/')
        else: form = ClientForm()
    return render_to_response('edit_client.html', {'form': form},  context_instance=RequestContext(request))

#edit_client.html
{% extends "base.html" %}

{% block content %}
<font face="verdana,news gothic,arial,heltevica,serif">
    <h3>Edit Client</h3>
</font>
<form method= "POST" action="">
    <font face="verdana,news gothic,arial,heltevica,serif">
    <div id="form">
        <table>
            {{form.as_table}}
        </table>
        <div align="center" STYLE=" margin-right:190px">
            <input type="submit" value="Submit" STYLE="background-color:#E8E8E8; color:#181818 "/>
        </div>
    </div>
</form>
{% endblock %}
  • 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-17T06:55:09+00:00Added an answer on May 17, 2026 at 6:55 am

    This will always run:

    return render_to_response('edit_client.html', {'form': form}
    

    But if request.method is not POST, nothing is assigned to form.

    Fixed code:

    @login_required 
    def edit_client(request, id=1):
        clients_list = Client.objects.filter(pk=id)  
        form = ClientForm()
        if request.method == 'POST':
           form = ClientForm(request.POST or None)
           if form.is_valid():
               form.save()
               return HttpResponseRedirect('/index/clients/')
        return render_to_response('edit_client.html', {'form': form},  context_instance=RequestContext(request))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Django app in which the users can create reports at various
I want to create a django app that can upload and display images. However
I'm using django with app-engine-patch and I'm having this wierd problem running manage.py dumpdata
I have a Django that having API for JSON, and I want it to
I'm a django newbie and have been having a problem. In my project root
I'm having a problem using get_model and get_models in django I have several models
I seem to be having a problem. I want a view where I can
I'm having a problem with a ajax request in a django project. I have
I am having problems with django forms and image uploads. I have googled, read
I've set up django with haystack but im having a few problems. I have

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.