简体   繁体   English

Django Crispy表单将无法保存/提交

[英]Django Crispy form will not save/submit

I am unable to get the Submit to work with crispy-froms. 我无法使“提交”与crisp-froms一起使用。 Normal django forms with bootstrap works fine. 带有引导程序的普通django形式可以正常工作。 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. 单击sumbit时,它将打开我的客户概述页面,但未添加任何新客户。 Not all fields are shown here but the field settings are all set to allow null values. 此处未显示所有字段,但所有字段设置均设置为允许空值。

My models.py 我的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 我的views.py

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

my forms.py 我的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 编辑:打开表单时,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 output when saving 保存时的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

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. 因此,如果是POST,请检查该表单是否有效并保存。 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. 这也避免了将“ / customeroverview /”设置为表单操作。 If you really want to post to this url, add the customeroverview view to your question. 如果您确实要发布到此URL,则将customeroverview视图添加到您的问题中。 Btw, I would advice you to use django's reverse function to create your urls. 顺便说一句,我建议您使用Django的reverse功能创建您的网址。 ( https://docs.djangoproject.com/en/dev/ref/urlresolvers/#reverse ). 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 您视图中有关模型形式的更多文档: https : //docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-model-formset-in-a-view

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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