我只是遇到了一个Crispy表单的问题;我不能加载一个按钮。我已经检查了大量的其他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。对我来说什么都不管用。
设置:
Django: 3.2.5
易碎-表单: 1.12.0
发布于 2021-07-14 00:17:43
好吧,我想通了,但我不知道为什么它会这样。
class ExampleModelForm(forms.ModelForm):
helper = FormHelper()
helper.add_input(Submit('submit', 'Submit'))
class Meta:
model = ExampleModel
fields = ['content', 'title']
基本上你不需要def__init__(...
,有人能给我解释一下吗?
https://stackoverflow.com/questions/68370487
复制