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 8606675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:09:21+00:00 2026-06-12T03:09:21+00:00

I have model MyModel as listed below. When I submit the form, in /

  • 0

I have model MyModel as listed below. When I submit the form, in /, I got aKeyError at /add with exception value name. According to debugger, the error is in this line in views.form_add:

name = request.session['name']

What is wrong?

myapp/models.py

class MyModel(models.Model):
    name = models.CharField(max_length=50)

myapp/forms.py

class MyModelForm(ModelForm):
    class Meta:
        model = MyModel

myapp/urls.py

urlpatterns = patterns('myapp.views',
    url(r'^$', 'main'),
    url(r'^add/', 'form_add'),
)

myapp/views.py

def main(request):
    if request.method == 'POST':
        form = MyModelForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
        return HttpResponseRedirect('/add') # Redirect after POST
    else:
        form = MyModelForm()
        args = {}
        args['form'] = form
    return render(request, 'main.html', args)

def form_add(request):
    args = {}
    name = request.session['name']
    args['name'] = name
    mm = MyModel(name=name)
    mm.save()

    return render(request, 'add.html', args)

templates/main.html

<form method="POST" id="" action="">
  {% csrf_token %}
  {{ form.as_p }}
  <button>Submit</button>
</form>

templates/add.html

<p>{{ name }}</p>

Edition 1

myproject/settings.py (template context processors)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
    'django.core.context_processors.static',
    )

Edition 2 – solved

I rewrote my main function saving MyModel and request.session['name'] as below.

def main(request):
    if request.method == 'POST':
        form = MyModelForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            # I added the lines below to create MyModel and request
            request.session['name'] = name
            mm = MyModel.objects.create(name=name)
            mm.save()
            # the indentation was wrong
            return HttpResponseRedirect('/add') # Redirect after POST
    else:
        form = MyModelForm()
    # the indentation was wrong
    args = {}
    args['form'] = form
    return render(request, 'main.html', args)
  • 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-06-12T03:09:22+00:00Added an answer on June 12, 2026 at 3:09 am

    This is very strange code. When your form is submitted, if it is valid, you put the name value in a variable which is immediately thrown away. You don’t save the form, but again throw away the values. Whether or not the form is valid, you redirect immediately to the add/ URL, where you assume that there is a name value in the session, despite never having saved anything to the session previously.

    Your base problem is that there isn’t a name key in the session, but I’m confused about why you think there would be, given the code you’ve shown.

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

Sidebar

Related Questions

in a Model I have a CharField with choices: class MyModel(models.Model): THE_CHOICES=( ('val',_(u'Value Description')),
I have model public class MyModel { public string Name { get; set;} }
I have a Model that looks like: class MyModel(Model): name = models.TextField(blank=True, default='') bio
I have a model similar to this one: class MyModel(models.Model): name = models.CharField(max_length =
I have a model with an optional file field class MyModel(models.Model): name = models.CharField(max_length=50)
I have a model: class MyModel(models.Model): id = models.IntegerField(primary_key=True) recorded_on = models.DateField() precipitation =
I have a model with a datetime field: class MyModel(models.Model): created = models.DateTimeField(auto_now =
I have a model similar to this: public class myModel { public ClassA ObjectA
I have a standard ActiveRecord model with the following: class MyModel < ActiveRecord::Base custom_method
I have a model: class MyModel(models.Model): ... def save(self): print saving ... def delete(self):

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.