Crispy Form VariableDoesNotExist on Django

前端 未结 2 765
执笔经年
执笔经年 2021-01-05 05:15

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

2条回答
  •  余生分开走
    2021-01-05 06:15

    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)

提交回复
热议问题