简体   繁体   English

Django上的Crispy Form VariableDoesNotExist

[英]Crispy Form VariableDoesNotExist on Django

For crispy form on Django, I keep getting VariableDoesNotExist at / Failed lookup for key [form] in u'[{\\'False\\': False, \\'None\\': None,..... 对于Django上的crispy表单,我一直在VariableDoesNotExist at /找到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: 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 don't understand, because according to documentation I am using FormHelper on attribute helper so I should be able to use {% crispy form %} 我不明白,因为根据文档我在属性助手上使用FormHelper所以我应该能够使用{%crispy form%}

The first argument to the crispy template tag is the name of the context variable where Crispy Forms expects the Form instance. crispy模板标记的第一个参数是Crispy Forms期望Form实例的上下文变量的名称。 So you need to somehow get a Form instance in your template context. 因此,您需要以某种方式在模板上下文中获取Form实例。 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 ) (另请参阅自定义模板标记的常用设置过程

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 . 我遇到了VariableDoesNotExist问题以及Failed lookup for key [form] ,但对我来说问题是我错误地使用generic.DetailView作为基类而不是generic.UpdateView

Changing to UpdateView fixed the problem. 更改为UpdateView修复了该问题。

class MyUpdateView(generic.UpdateView):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM