0

I've read the other posts on here about form data not saving, but can't seem to pinpoint my own problem. Have a feeling its something really basic I'm missing. Really appreciate the help thanks.

#model

class AboutMe(models.Model):
    title = models.CharField(max_length=30)
    user = models.ForeignKey(User)
    post = models.TextField()
    created = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.title

#form

class AboutMeForm(forms.ModelForm):

    class Meta:
        model = AboutMe
        fields = ['title', 'post']

#view


def post_line(request):
    if request.method == 'POST':
        form = AboutMeForm(request.POST)
        if form.is_valid():
            new_about = form.save(commit=False)
            new_about.user = request.user       
            new_about.save()
            return HttpResponseRedirect('index.html')
    else:
        form = AboutMeForm()
    return render_to_response('post_line.html', locals(), context_instance=RequestContext(request))

#template:

<div class="post_line">
<form enctype="multipart/form-data" action='.' method="post">{% csrf_token %}
    {{ form.as_table }}
    <input type="submit" name="submit" value="post" />
</form>
</div>
Jonathan
  • 219
  • 1
  • 11
  • 18
  • 1
    What happens in your view if POST data is not valid? Looks like there is no form returned with errors. You should try to debug your view. – Jingo Aug 28 '12 at 17:54
  • Thanks Jingo! It works now after I added an else to the view if the POST form isn't valid. I dont understand why that would allow the form to save though? I mean if the data was initially valid shouldnt it save even without the else statement? – Jonathan Aug 28 '12 at 18:01
  • I am not sure whether I understand you in the right way, but you shouldn't save unvalid data. There is cleaned data attached to the form, you could inspect that, maybe it can help you if you read the chapter about forms in the online doc once more ... :) – Jingo Aug 28 '12 at 18:08

2 Answers2

0

I am not able to comment here so i am writing here

1) HttpResponseRedirect('index.html') this is wrong here you have to pass some url like

/login/ or /profile/

2) please print your form like

print form and check what you are getting is all fields are valid or anything missing

and then tell the form error

user1481793
  • 543
  • 2
  • 13
  • 21
0

Try doing this:

forms.py

class AboutMeForm(ModelForm):
    class Meta:
        model=AboutMe
        fields = YOUR FIELDS

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user', None)
        super(AboutMeForm, self).__init__(*args, **kwargs)

    def save(self, commit=True):
        instance = super(AboutMeForm, self).save(commit=False)
        if self.user:
            instance.user = self.user
        return instance.save()

on views.py:

def post_line(request):
    if request.method == 'POST':
        form = AboutMeForm(request.POST)
        if form.is_valid():
            new_about = form.save() 
            return HttpResponseRedirect('index.html')
    else:
        form = AboutMeForm()
    return render_to_response('post_line.html', locals(), context_instance=RequestContext(request))
vault
  • 3,930
  • 1
  • 35
  • 46
Walucas
  • 2,549
  • 1
  • 21
  • 44