[英]Django Crispy Forms; Does not load button
I'm just have an issue with Crispy Forms;我只是对 Crispy Forms 有问题; I cannot get a button to load.我无法加载按钮。 I've checked a ton of other StackOverflow forms, but all the examples I've followed, nothing seems to work.我已经检查了大量其他 StackOverflow 表单,但是我遵循的所有示例似乎都不起作用。
example forms.py
示例forms.py
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from django import forms
from .models import ExampleModel
class ExampleModelForm(forms.ModelForm):
def __int__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.add_input(Submit('submit', 'Submit'))
class Meta:
model = ExampleModel
fields = ['content', 'title']
example views.py
示例views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .forms import ExampleModelForm
def get_form(request):
if request.method == 'POST':
form = ExampleModelForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/success/')
else:
form = ExampleModelForm()
return render(request, 'example_form.html', {'form': form})
example example_form.html
示例example_form.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
{% crispy form %}
{% endblock %}
My form loads beautifully, but I just cannot get the button to show.我的表单加载得很漂亮,但我就是无法显示按钮。 I've looked at the docs and I don't see what I am doing differently.我查看了文档,但没有看到我在做什么不同。 Maybe it just doesn't play well with ModelForms?也许它不能很好地与 ModelForms 配合使用? I don't know.我不知道。
I've tried changing the location of def__init__...
to be below the Meta
class.我尝试将def__init__...
的位置更改为低于Meta
类。 I defined super()
to be super(ExampleModelForm, self)
and self.helper = FormHelper(self)
.我将super()
定义为super(ExampleModelForm, self)
和self.helper = FormHelper(self)
。
I even tried to change {% crispy form %}
to {% crispy form form.helper %}
in the html file.我什至尝试将 html 文件中的{% crispy form form.helper %}
{% crispy form %}
更改为{% crispy form form.helper %}
。 It just breaks.它只是打破了。
I tried following this example too.我也试过这个例子。 Just nothing is working for me.只是没有什么对我有用。
Settings:设置:
Django: 3.2.5姜戈:3.2.5
Crispy-Forms: 1.12.0脆皮形式:1.12.0
Ok I figured this out, but I don't know why it behaves like this.好的,我想通了,但我不知道为什么它会这样。
class ExampleModelForm(forms.ModelForm):
helper = FormHelper()
helper.add_input(Submit('submit', 'Submit'))
class Meta:
model = ExampleModel
fields = ['content', 'title']
Basically you don't need the def__init__(...
Could someone explain this to me?基本上你不需要def__init__(...
有人可以向我解释一下吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.