Reputation: 349
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>
Upvotes: 0
Views: 888
Reputation: 599490
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.
Upvotes: 1