Crispy VariableDoesNotExist Shape on Django

For a crispy shape on Django I keep getting VariableDoesNotExist at / Failed lookup for key [form] in u'[{\'False\': False, \'None\': None,.....

{% extends 'base.html' %}
{% load crispy_forms_tags %}

{% block loginForm %}
    <div class="container" style="padding-bottom: 70px;">
        <div class='row'>
            <div class='col-md-6 col-md-offset-3'>
                <div class="well">
                    <legend>Sign in</legend>
                    <form method="post" action="{% url 'django.contrib.auth.views.login' %}" class="form-horizontal">
                        {% crispy form %}
                        <input type="hidden" name="next" value="{{ next }}"/>
                    </form>
                </div>
            </div>
        </div>
    </div>

{% endblock loginForm %}

forms.py:

from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Submit, HTML, Button, Row, Field, Hidden, Fieldset
from crispy_forms.bootstrap import AppendedText, PrependedText, FormActions
from django.contrib.auth.forms import AuthenticationForm


class LoginForm(AuthenticationForm):
    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-2'
        self.helper.field_class = 'col-lg-8'
        self.helper.form_tag = False
        self.helper.layout = Layout(
            Field('username', placeholder="username", css_class='input-xlarge'),
            Field('password', placeholder="Password", css_class='input-xlarge'),
            FormActions(
                Submit('login', 'Login', css_class="btn-primary"),
            )
        )

I do not understand, because according to the documentation I use FormHelper as an attribute helper so that I can use {% crispy form%}

+4
source share
2 answers

The first argument to the crispytemplate tag is the name of the context variable where Crispy Forms expects an instance Form. Therefore, you need to somehow get the instance Formin the context of the template. If you used this form in a view, you could do something like

def yourview(request):
    return TemplateResponse(request, "yourtemplate.html", {'form': LoginForm()})

, :

@register.inclusion_tag('path/to/login_form.html')
def display_login_form():
    return {'form': LoginForm()}

:

{% load your_template_tags %}
{% display_login_form %}

(. )

+8

VariableDoesNotExist Failed lookup for key [form], , generic.DetailView generic.UpdateView.

UpdateView .

class MyUpdateView(generic.UpdateView):

    template_name = "object_update.html"
    model = MyModel
    form_class = MyCreateForm
+1

All Articles