我只是对 Crispy Forms 有问题;我无法加载按钮。我已经检查了大量其他 StackOverflow 表单,但我遵循的所有示例似乎都不起作用。
示例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']
示例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_form.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
{% crispy form %}
{% endblock %}
我的表单加载得很漂亮,但我无法让按钮显示。我查看了 docs 并没有发现我在做什么不同。也许它不能很好地与 ModelForms 配合使用?我不知道。
我已尝试将 def__init__...
的位置更改为低于 Meta
类。我将 super()
定义为 super(ExampleModelForm, self)
和 self.helper = FormHelper(self)
。
我什至尝试将 html 文件中的 {% crispy form %}
更改为 {% crispy form form.helper %}
。它只是坏了。
我也尝试关注此 example。只是没有什么对我有用。
设置:
姜戈:3.2.5
脆皮形式:1.12.0
答案 0 :(得分:0)
好的,我想通了,但我不知道为什么会这样。
class ExampleModelForm(forms.ModelForm):
helper = FormHelper()
helper.add_input(Submit('submit', 'Submit'))
class Meta:
model = ExampleModel
fields = ['content', 'title']
基本上你不需要 def__init__(...
有人可以向我解释一下吗?