5

New to Django and I have exhausted every forum and tutorial I can and I am still having problems getting my form data to the database. I have a simple model that consists of a name and an email field that I will eventually reference in the program. I can load different pages after clicking submit, but my data will not post to the database. I have tried everything I can think of, so my code is probably jacked at this point, but in the current iteration this is what I have:

#models.py
    from django.db import models
from django.forms import ModelForm

class Patron(models.Model):
    name = models.CharField(max_length=200)
    email = models.EmailField(max_length=75)


    def _unicode_(self):
        return self.name

class PatronForm(ModelForm):
    class Meta:
        model = Patron

#view.py

from django.shortcuts import render_to_response, get_object_or_404
from patrons.models import Patron 
from django.template import RequestContext
from django.core.context_processors import csrf
from django.core.urlresolvers import reverse
from django.forms import ModelForm


def index(request):
    if request.method == 'POST':
        post = request.POST
        name = post['name']
        email = post['email']
        f = PatronForm(request.Post)
        new_patron = f.save()
    return render_to_response('patrons/index.html',
                               context_instance=RequestContext(request))

#html
 <body>
    <h1>/Picture Taker/</h1>



    <form aciton="." name="patron" method="post" >
    {% csrf_token %}

        <label>
            <div>name</div> <input type="text" name="name" id="name" value="{{name}}">
        </label>
        <label>
            <div>email</div> <input type="text" name="email" id="email" value="{{email}}">
        </label>

        <div class="error">{{error}}</div>
        <input type="submit" value="Submit">
    </form>

</body>

Any help would be greatly appreciated

2 Answers 2

4

All of

post = request.POST
name = post['name']
email = post['email']
f = PatronForm(request.Post)
new_patron = f.save()

can be re-written as (note the case of request.POST):

f = PatronForm(request.POST)
new_patron = f.save()

but you should be checking for errors in the form before saving, so:

f = PatronForm(request.POST)
if f.is_valid():
    new_patron = f.save()

You also have a typo in your form tag, it should be "action", not "aciton". The {{ error }} you have in your template doesn't refer to anything present in your view. While debugging, it might be helpful to let the form render itself, like:

{{ form.as_p }}

so you can see any errors in the form submission.

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

2 Comments

Thanks@Tom Now I get this error when I try to submit global name 'PatronForm' is not defined 11 f = PatronForm(request.POST) ... ▶ Local vars
In views.py, you need to import your form. It should really be in a separate file called "forms.py" for consistency, but in your case it looks like "from patron.models import PatronForm"
0

You don't need "every forum and tutorial", you just need the official documentation, where this is explained clearly. The only change from the code you see at that link is that you need to add form.save() inside the bit that says "Process the data" (and, you should use RequestContext in the render_to_response call, as you already do).

The other thing to change is that you should rely on Django to render the whole field in the template, not just the value:

<p>
{{ form.name }} 
{{ form.name.errors }}
</p>

<p>
{{ form.email }}
{{ form.email.errors }}
</p>

2 Comments

thanks, I'm getting the following after making changes to my code: global name 'PatronForm' is not defined with the following errors Traceback: File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/Users/curtis/picturetaketest3/mysite/patrons/views.py" in index 11. f = PatronForm(request.POST)
Well, you haven't imported it into your view.

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.