dimanche 28 juin 2015

Django Inclusion Tag doesn't post to database

I'm trying to build a form to save Names and Email Adresses to my database. However, it doesn't save... I've used an Inclusion Tag because I want to use the same form in different templates. This is my models.py:

class Contact(models.Model):
    FRAU = 'FR'
    HERR= 'HR'
    GENDER_CHOICES = (
        (FRAU, 'Frau'),
        (HERR, 'Herr'),
    )
    gender = models.CharField(max_length=2, choices=GENDER_CHOICES, default=FRAU)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=200)
    email = models.EmailField()

    def __unicode__(self):
        return "%s %s" %(self.first_name, self.last_name)

This is my forms.py:

class FragenContactForm(ModelForm):
    class Meta:
        model = Contact
        fields = ['gender', 'first_name', 'last_name', 'email']

This is my custom tags module:

from django import template
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from fragen.forms import FragenContactForm


register = template.Library()

@register.inclusion_tag('fragen/askforoffer.html', takes_context=True)
    def askforoffer(context):
    form = FragenContactForm(context['request'].POST or None)
    if context['request'].method=='POST':
        if form.is_valid():
            form.save()
        return HttpResponseRedirect(reverse('fragen/thanks.html'))
    else:
        messages.error(context['request'], "Error")
    return {'form': FragenContactForm()}

After I fill in and submit the form, I see nothing in my database. Am I missing something? Thanks!

Aucun commentaire:

Enregistrer un commentaire