Reputation: 1192
I have built a crispy form from a tutorial (https://godjango.com/29-crispy-forms/) and am getting an error that I believe means I need to define a URL in the urls.py. I also get the sense that there may be more than one issue going on - I am still trying to make this work and will continue to research it but I am quite new to Django and Python so struggling on this. Any guidance gratefully received.
Here's the error:
Failed lookup for key [form] in "[{'True': True, 'False': False, 'None': None}, {}, {}, {'view': <django.views.generic.base.TemplateView object at 0x10faa03c8>, 'home_url': '/'}]"
For reference here are the files:
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
and the section on home.html
<section id="contact" class="contact">
<div class="container">
<div class="row">
{% crispy form %}
</div>
</div>
</section>
I have {% load crispy_forms_tags %} at the top of the home.html.
Upvotes: 1
Views: 1180
Reputation: 1425
It seems that your route '/'
couldn't find any form.You can try by changing TemplateView
class and put your MainView
class.
urlpatterns = [
url(r'^$', MainView.as_view(), name='home'),
]
Then no need to define template name because it is defined on your MainView
class.
Hope it will help.
Upvotes: 1