我已经从教程(https://godjango.com/29-crispy-forms/)构建了一个脆弱的表单,我收到一个错误,我认为这意味着我需要在urls.py中定义一个URL。我也感觉到可能存在不止一个问题 - 我仍在努力使这项工作继续研究它,但我对Django和Python很陌生,因此在这方面苦苦挣扎。感激地收到任何指导。
这是错误:
Failed lookup for key [form] in "[{'True': True, 'False': False, 'None': None}, {}, {}, {'view': <django.views.generic.base.TemplateView object at 0x10faa03c8>, 'home_url': '/'}]"
这里有参考文件:
forms.py
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Field
from crispy_forms.bootstrap import (
PrependedText, PrependedAppendedText, FormActions)
class SimpleForm(forms.Form):
username = forms.CharField(label="Username", required=True)
password = forms.CharField(
label="Password", required=True, widget=forms.PasswordInput)
remember = forms.BooleanField(label="Remember Me?")
helper = FormHelper()
helper.form_method = 'POST'
helper.add_input(Submit('login', 'login', css_class='btn-primary'))
class CartForm(forms.Form):
item = forms.CharField()
quantity = forms.IntegerField(label="Qty")
price = forms.DecimalField()
helper = FormHelper()
helper.form_method = 'POST'
helper.layout = Layout(
'item',
PrependedText('quantity', '#'),
PrependedAppendedText('price', '$', '.00'),
FormActions(Submit('login', 'login', css_class='btn-primary'))
)
class CreditCardForm(forms.Form):
fullname = forms.CharField(label="Full Name", required=True)
card_number = forms.CharField(label="Card", required=True, max_length=16)
expire = forms.DateField(label="Expire Date", input_formats=['%m/%y'])
ccv = forms.IntegerField(label="ccv")
notes = forms.CharField(label="Order Notes", widget=forms.Textarea())
helper = FormHelper()
helper.form_method = 'POST'
helper.form_class = 'form-horizontal'
helper.label_class = 'col-sm-2'
helper.field_class = 'col-sm-4'
helper.layout = Layout(
Field('fullname', css_class='input-sm'),
Field('card_number', css_class='input-sm'),
Field('expire', css_class='input-sm'),
Field('ccv', css_class='input-sm'),
Field('notes', rows=3),
FormActions(Submit('purchase', 'purchase', css_class='btn-primary'))
)
views.py
from django.views.generic import FormView
from forms import SimpleForm, CreditCardForm, CartForm
class MainView(FormView):
template_name = "pages/home.html"
form_class = SimpleForm
urls.py
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic import TemplateView
from django.views import defaults as default_views
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name='home'),
url(r'^page/$', TemplateView.as_view(template_name='pages/page.html'), name='page'),
# Django Admin, use {% url 'admin:index' %}
url(settings.ADMIN_URL, admin.site.urls),
# User management
url(r'^users/', include('base_django_template.users.urls', namespace='users')),
url(r'^accounts/', include('allauth.urls')),
# Your stuff: custom urls includes go here
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
# This allows the error pages to be debugged during development, just visit
# these url in browser to see how these error pages look like.
urlpatterns += [
url(r'^400/$', default_views.bad_request, kwargs={'exception': Exception('Bad Request!')}),
url(r'^403/$', default_views.permission_denied, kwargs={'exception': Exception('Permission Denied')}),
url(r'^404/$', default_views.page_not_found, kwargs={'exception': Exception('Page not Found')}),
url(r'^500/$', default_views.server_error),
]
if 'debug_toolbar' in settings.INSTALLED_APPS:
import debug_toolbar
urlpatterns = [
url(r'^__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
以及home.html上的部分
<section id="contact" class="contact">
<div class="container">
<div class="row">
{% crispy form %}
</div>
</div>
</section>
我在home.html的顶部有{%load crispy_forms_tags%}。
答案 0 :(得分:1)
您的route '/'
似乎无法找到任何表单。您可以尝试更改TemplateView
课程并将MainView
课程。{/ p>
urlpatterns = [
url(r'^$', MainView.as_view(), name='home'),
]
然后无需定义模板名称,因为它是在MainView
类上定义的。
希望它会有所帮助。