gpt4 book ai didi

python - Django Crispy 表单提交按钮

转载 作者:行者123 更新时间:2023-11-28 20:03:17 24 4
gpt4 key购买 nike

我正在尝试使用 Crispy Forms 使我的表单看起来不错。我的 forms.py 中有以下内容:

from django import forms
from .models import Team
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Field
from crispy_forms.bootstrap import (
PrependedText, PrependedAppendedText, FormActions)

class CreateTeamForm(forms.ModelForm):
class Meta:
model = Team

fields = [ 'Project_name', 'Project_number'
]

helper = FormHelper()
helper.add_input(Submit('submit', 'Submit', css_class='btn-primary'))

helper.form_method = 'POST'

然后在我的 views.py 中:

def create_team(request):
if request.method == 'POST':
form = CreateTeamForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return render('/teams/my_team.html',{''})
else:
form = CreateTeamForm()

return render(request, 'teams/team_form.html', {'form':CreateTeamForm()})

最后在我的模板中:

{% extends "main/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="row">
<div class="jumbotron">
{% crispy form %}

</div>
</div>
{% endblock %}

但是,我调用的提交按钮没有显示。我已经阅读了 cripsy 表单文档,我似乎无法发现我的实现有任何问题。显示除提交按钮以外的所有内容。有什么想法吗?

最佳答案

您的 FormHelper 应该是您的表单类的属性,而不是它的 Meta 类。缩减代码的那部分:

class CreateTeamForm(forms.ModelForm):
class Meta:
model = Team

fields = [ 'Project_name', 'Project_number' ]

helper = FormHelper()
helper.add_input(Submit('submit', 'Submit', css_class='btn-primary'))
helper.form_method = 'POST'

关于python - Django Crispy 表单提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42331470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com