0

When I submit the form my terminal says: "POST /polls/ HTTP/1.1" 200 851. When I check through python manage.py shell, the form data does not show up. I am not sure why the data is not saving to the db, which is sqlite. I think the error is in the view section when I try to save the form. I have read through different post, which seem to have similar issues, but I can't seem to figure out what my issue could be.

Model:

from django.db import models
class Stores(models.Model):
    name  = models.CharField(max_length=200)
    address = models.CharField(max_length=30)
    city = models.CharField(max_length=30)
    state = models.CharField(max_length=2)
    def __str__(self):
        return "%s (%s,%s) %s" % (self.name, self.city, self.state, 
        self.address)

Forms:

from django.forms import ModelForm
from mysite.polls.models import Stores

   class StoreForm(ModelForm):
       class Meta:
           model = Stores
           fields = ['name','address','city','state']

Views:

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse

from mysite.polls.models import Stores
from mysite.polls.forms import StoreForm

def index(request):
    downtown_store = Stores.objects.get(name="Corporate")
    store_name = downtown_store.name
    store_address = downtown_store.address
    store_state = downtown_store.state
    if request.method == 'Post':
        form = StoreForm(request.POST)
            if form.is_valid():
               form.save(commit=True)
               return HttpResponseRedirect(reverse('index'))
   else:
        form = StoreForm() 

       context = {'store_name':store_name, 'store_address':store_address, 'store_state':store_state, 'form':form,} 

       return render(request,'polls/index.html',context)

Templates:

    <html>
    <body>
        <h1> {{store_name}} </h1>
        <h2> {{store_address}} </h2>
        <h3> {{store_state}} </h3>

    <form action="{% url "index" %}"  method="post">
      {% csrf_token %}
      {{ form }}
      <input type="submit" value="Submit" />
   </form>

1
  • No, I was just testing how to pull data from the DB and display in a template. Thanks for the information, good to know for the future. Commented Jun 15, 2017 at 21:44

1 Answer 1

1

The method needs to be in all caps.

if request.method == 'POST':

Note, the last two lines of your view need to be moved one indent to the left.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.