user808996
user808996

Reputation: 179

Using ModelForm in Django to add to database

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

Upvotes: 5

Views: 9425

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599600

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>

Upvotes: 0

Tom
Tom

Reputation: 22841

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.

Upvotes: 4

Related Questions