Django Crispy表格不会保存/提交

时间:2014-10-07 22:44:11

标签: python django twitter-bootstrap django-crispy-forms

我无法让提交与crispy-froms一起使用。正常的django形式与bootstrap工作正常。我已经尝试了所有可以找到的教程,但此时无法找到代码有什么问题。

点击sumbit时会打开我的客户概述页面,但没有添加新客户。此处并未显示所有字段,但字段设置均设置为允许空值。

我的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

编辑:打开表单时的CMD输出

[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输出

[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

1 个答案:

答案 0 :(得分:1)

您的观点不合适。目前,您只需创建一个表单并进行渲染。发布表单时没有任何操作,只是再次呈现。因此,如果是POST,请检查表单是否有效并保存。所以这将使它像:

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))

这也可以避免您设置' / customeroverview /'作为形式行动。如果您确实要发布到此网址,请在您的问题中添加customeroverview视图。顺便说一句,我建议你使用django的reverse函数来创建你的网址。 (https://docs.djangoproject.com/en/dev/ref/urlresolvers/#reverse)。

有关视图中的模型形式的更多文档:https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-model-formset-in-a-view