Django Crispy 表单提交按钮
Django Crispy Form Submit Button
我正在尝试使用 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
应该是您表单 class 的属性,而不是其 Meta
class。缩进你的那部分代码:
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'
这个解决方案对我有用
from django.forms import ModelForm
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
class BillForm(ModelForm):
def __init__(self, *args, **kwargs):
super(BillForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.add_input(Submit('submit', 'Submit', css_class='btn-primary'))
self.helper.form_method = 'POST'
class Meta:
model = Bill
fields = ('__all__')
你在这个例子之后的 __init__
方法中定义你的助手,然后你把它放在你的模板中:
{% load crispy_forms_tags %}
{% crispy form %}
在你的view/action中你应该有类似的东西
form = BillForm()
我正在尝试使用 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
应该是您表单 class 的属性,而不是其 Meta
class。缩进你的那部分代码:
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'
这个解决方案对我有用
from django.forms import ModelForm
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
class BillForm(ModelForm):
def __init__(self, *args, **kwargs):
super(BillForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.add_input(Submit('submit', 'Submit', css_class='btn-primary'))
self.helper.form_method = 'POST'
class Meta:
model = Bill
fields = ('__all__')
你在这个例子之后的 __init__
方法中定义你的助手,然后你把它放在你的模板中:
{% load crispy_forms_tags %}
{% crispy form %}
在你的view/action中你应该有类似的东西
form = BillForm()