【问题标题】:Unable to Manually set Form Attributes with Crispy Forms无法使用 Crispy 表单手动设置表单属性
【发布时间】:2016-02-28 23:32:13
【问题描述】:

我一直致力于使用 django-crispy-forms、Django 1.8.4 和 django-crispy-forms-1.5.2 创建模型表单。我无法更改表单标签属性。

我已经尝试设置self.helper.form_tag = False,但它仍然会产生<form> 标记。我试过设置其他属性,比如form_action,但这也不起作用,表单标签保持不变(最终的HTML仍然只是<form>)。

views.py

class RegisterStudentView(CreateView):

    template_name = "register_student.html"
    model = Student
    form_class = StudentRegistrationForm

    def form_valid(self, form):
        form.save()
        return HttpResponseRedirect('dashboard')

forms.py

class StudentRegistrationForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(StudentRegistrationForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_tag = False

    class Meta:
        model = Student
        exclude = ['is_active', 'is_overdue', 'personid', 'tertiary_cell']

任何帮助将不胜感激。

【问题讨论】:

  • 您的 html 模板中有什么?表单标签可能在那里而不是由crispy-forms生成的
  • 谢谢@awwester,这正是我的问题。我对脆皮表格的调用周围有一个表格标签。

标签: python django forms django-crispy-forms


【解决方案1】:

正如 awwester 评论的那样,问题在于脆皮表单功能周围有一个硬编码的 <form> 标签:

<form>
  {% crispy %}
</form>

基本上,脆皮表单使用现有的表单标签,并没有创建我想要的新标签。

【讨论】:

    猜你喜欢
    • 2020-04-07
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多