Crispy Form VariableDoesNotExist on Django

前端 未结 2 744
执笔经年
执笔经年 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 05:59

    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
    
    0 讨论(0)
  • 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)

    0 讨论(0)
提交回复
热议问题