【问题标题】:Django Crispy form will not save/submitDjango Crispy 表单不会保存/提交
【发布时间】:2014-10-07 22:44:10
【问题描述】:

我无法让 Submit 与crispy-froms 一起使用。带有引导程序的普通 django 表单可以正常工作。我已经尝试了所有我能找到的教程,但目前无法找到代码有什么问题。

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

我的模型.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)

我的意见.py

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

我的表单.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

【问题讨论】:

  • 您在服务器端遇到了哪些错误(如果有)?如果通过“终端”或 CL 运行服务器,您可以分享提交表单后从那里获得的输出吗?另外,您是否尝试过从前端诊断任何问题(通过浏览器终端中的错误)?
  • 嗨,乔,我在问题中添加了终端输出。保存时浏览器没有显示任何错误 - 我在 settings.py 中启用了 no Debug 选项

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


【解决方案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

【讨论】:

    猜你喜欢
    • 2017-07-08
    • 1970-01-01
    • 2012-07-16
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多