0

For some reason, I can't get form.save() to save to my database. I'm able to create the form, and have the form pass itself off to my template, but nothing is getting saved to the database. I've mucked around with it for many hours and haven't been able to get it to work.

Any help is appreciated.

Here is the relevant code..

This is my add/model.py

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

class addTask(models.Model):
    task = models.CharField('Task',  max_length=60)
    taskNotes = models.CharField('Notes', max_length=600)

    def __unicode__(self):
        return self.task

class addTaskForm(ModelForm):
    class Meta:
        model = addTask

template/addTHEtask.html. This is getting referenced correctly.

    <form action="/todo/" method="post">
    {{ form.as_p }}
    <input type="submit" value="Add Task" />
    </form>

add/views.py

from django.shortcuts import render_to_response
from django.template import RequestContext
from myToDo.add.models import addTask, addTaskForm

def create_task(request):
    if request.method == 'POST':
        form = addTaskForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = addTaskForm()
    return render_to_response('addTHEtask.html', {'form': form})
Greg Cain
  • 113
  • 2
  • 6
  • 4
    are you sure the form is validating and that form.save() is definitely getting called? Also, it's hard to tell from the code you posted, but is class Meta definitely indented correctly i.e. it's a inner class of addTaskForm? Finally, as an aside, it's convention to name your classes & forms with a capital; AddTask and AddTaskForm – Timmy O'Mahony Jun 03 '11 at 23:33
  • And are you sure that you're saving the data in the database? (i.e. did you forget to commit the transaction?) – Sean Jun 03 '11 at 23:40
  • What @pastylegs said - and one way to check it, is to add a {{ form.errors }} variable to your template, see what shows up. – John C Jun 04 '11 at 00:18
  • As pastylegs said, make sure you have everything indented correctly. What you've posted here is not indented correctly. – mohi666 Jun 04 '11 at 00:21
  • 2
    unrelated but you should use csrf token for better security. https://docs.djangoproject.com/en/dev/ref/contrib/csrf/ – Eva611 Jun 04 '11 at 17:26
  • Try adding an `else` clause for `if form.is_valid()` to help debug, perhaps something like `else: print form; raise ValueError` – Tim McNamara Jun 05 '11 at 22:38
  • @pastylegs - Thanks for the tip naming conventions. Will use for the future. – Greg Cain Jun 05 '11 at 22:39
  • @pastylegs and others - the indention is correct in the code, and I've corrected the code above to reflect what it looks like. – Greg Cain Jun 05 '11 at 22:40
  • @sean - I explicitly called form.save(commit=true) (which is the default) and it made no difference. – Greg Cain Jun 05 '11 at 22:41
  • @Greg, have you looked at the database logs to see what's actually happening from the database's perspective? http://stackoverflow.com/questions/722221/how-to-log-postgres-sql-queries – Sean Jun 05 '11 at 22:59

1 Answers1

1

To properly debug your code, change your template to:

<form action="/todo/" method="post"> {{ csrf_token }}
    {{ form.errors }}
    {{ form.as_p }}
    <input type="submit" value="Add Task" />
</form>

And your view to:

def create_task(request):
    if request.method == 'POST':
        form = addTaskForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = addTaskForm()
    return render_to_response(
                     'addTHEtask.html', 
                     {'form': form}, 
                     context_instance=RequestContext(request))

I don't think the context_instance will do anything significant for you, but it is usually the right thing to pass when using render_to_response.

Showing the errors in the form may help you track down what the actual problem is. Your code looks (mostly) correct, except the missing csrf_token. Adding the token, and displaying any errors, should show you what is going wrong.

Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164