For crispy form on Django, I keep getting VariableDoesNotExist at /
Failed lookup for key [form] in u\'[{\\\'False\\\': False, \\\'None\\\': None,.....<
I came across the VariableDoesNotExist
problem as well with Failed lookup for key [form]
, but for me the problem was that I mistakenly used generic.DetailView
as base class instead of generic.UpdateView
.
Changing to UpdateView
fixed the problem.
class MyUpdateView(generic.UpdateView):
template_name = "object_update.html"
model = MyModel
form_class = MyCreateForm
The first argument to the crispy
template tag is the name of the context variable where Crispy Forms expects the Form
instance. So you need to somehow get a Form
instance in your template context. If you were using this form in a view, you could do something like
def yourview(request):
return TemplateResponse(request, "yourtemplate.html", {'form': LoginForm()})
If you want to have that form on many different pages, I'd suggest an inclusion tag:
@register.inclusion_tag('path/to/login_form.html')
def display_login_form():
return {'form': LoginForm()}
And in your template:
{% load your_template_tags %}
{% display_login_form %}
(see also the usual setup procedure for custom template tags)