在这个最新的项目中,我使用的是一个简单的联系表单,它解析为url / email /。我还想在主页上包含此表单。过去我在视图中使用了一个页面类时使用了get_context_data,但是我第一次使用带有crispy表单的cookiecutter并且不知道该怎么做,或者即使这是正确的做。
这是forms.py:
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Submit, HTML, Button, Row, Field
from crispy_forms.bootstrap import AppendedText, PrependedText, FormActions
from crispy_forms.layout import Submit
class ContactForm(forms.Form):
contact_name = forms.CharField(required=True)
contact_email = forms.EmailField(required=True)
content = forms.CharField(required=True, widget=forms.Textarea)
# the new bit we're adding
def __init__(self, *args, **kwargs):
super(ContactForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
Field('contact_name', placeholder="Full Name", autofocus=""),
Field('contact_email', placeholder="Email Address"),
Field('content', placeholder=""),
Submit('sign_in', 'Submit', css_class="btn btn-info"),
)
self.fields['contact_name'].label = "Your name:"
self.fields['contact_email'].label = "Your email:"
self.fields['content'].label = "Your message:"
......并且完美呈现在/ email /。如何让这个表单出现在头版?
如果需要,这是urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^email/$', views.email, name='email'),
url(r'^success/$', views.success, name='success'),
]
答案 0 :(得分:0)
您可以执行以下操作:
"
{% for field in form %}
<div class="form-group">
<div class="inner form">
<span class="text-danger small">{{ field.errors }}</span>
</div>
<label class="control-label col-sm-2" for="song_title">{{
field.label_tag }}</label>
<div class="col-sm-10">{{ field }}</div>
</div>
{% endfor %}
2。使用首页模板上的以下内容在首页中添加此模板
{% include 'form.html' %}
我希望这会有所帮助。