简体   繁体   中英

Django Crispy form will not save/submit

I am unable to get the Submit to work with crispy-froms. Normal django forms with bootstrap works fine. I have tried all the tutorials i could find and are at this time unable to find what is wrong with the code.

When clicking on sumbit it opens my customer overview page, but no new customer has been added. Not all fields are shown here but the field settings are all set to allow null values.

My models.py

from django.db import models
from django.utils.encoding import smart_unicode

class CustomerType(models.Model):
    customer_type = models.CharField(max_length=120, null=True, blank=True)
    timestamp_created = models.DateTimeField(auto_now_add=True, auto_now=False)
    timestamp_updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self):
        return smart_unicode(self.customer_type)

class Customer(models.Model):
    customer_type = models.ForeignKey(CustomerType, null=True, blank=True)
    customer_name = models.CharField(max_length=120, null=True, blank=True)

my views.py

def customercrispy(request): 
    form = ExampleForm()
    return render_to_response("customer-crispy.html",
                              {"example_form": form},
                              context_instance=RequestContext(request))

my forms.py

from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit, Div, Field
from crispy_forms.bootstrap import TabHolder, Tab, FormActions
from .models import Customer

class CustomerAddForm(forms.ModelForm):
    class Meta:
        model = Customer

class ExampleForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ExampleForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.form_method = 'post'
        self.helper.form_action = '/customeroverview/'     
        self.helper.add_input(Submit('submit', 'Submit'))

    class Meta:
        model = Customer

EDIT: CMD output when opening form

[08/Oct/2014 12:45:12] "GET /customercrispy/ HTTP/1.1" 200 11203
[08/Oct/2014 12:45:12] "GET /customercrispy/static/js/ie-emulation-modes-warning.js HTTP/1.1" 404 3118
[08/Oct/2014 12:45:12] "GET /assets/js/ie10-viewport-bug-workaround.js HTTP/1.1" 404 3079

CMD output when saving

[08/Oct/2014 12:46:52] "POST /customeroverview/ HTTP/1.1" 200 5129
[08/Oct/2014 12:46:52] "GET /customeroverview/static/js/ie-emulation-modes-warning.js HTTP/1.1" 404 3124
[08/Oct/2014 12:46:52] "GET /assets/js/ie10-viewport-bug-workaround.js HTTP/1.1" 404 3079

Your view isn't ok. At the moment you only create a form and render it. Nothing is done when the form is posted, it is just rendered again. So in case of a POST, check if the form is valid and save it. So that will make it something like:

def customercrispy(request):
    if request.method == 'POST': 
        form = ExampleForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('customercripsy') # name of view stated in urls
    else:
        form = ExampleForm()

    return render_to_response("customer-crispy.html",
                          {"example_form": form},
                          context_instance=RequestContext(request))

This also avoids you to set '/customeroverview/' as form action. If you really want to post to this url, add the customeroverview view to your question. Btw, I would advice you to use django's reverse function to create your urls. ( https://docs.djangoproject.com/en/dev/ref/urlresolvers/#reverse ).

More documentation about modelforms in your view: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-model-formset-in-a-view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM