简体   繁体   English

Django crispy表单:在模板'bootstrap3 / whole_uni_form.html'中解析变量'form_html'时出现异常

[英]Django crispy forms: Exception while resolving variable 'form_html' in template 'bootstrap3/whole_uni_form.html'

I'm making my first Django app and I decided to use Crispy forms. 我正在制作我的第一个Django应用程序,我决定使用Crispy表单。 When I'm accessing my view, it throws a bunch of weird errors (I PasteBinned them because the listing is really, really big). 当我访问我的视图时,它会抛出一堆奇怪的错误 (我粘贴它们,因为列表确实非常非常大)。 The strange thing is, the form actually renders correctly. 奇怪的是,表单实际上正确呈现。

My form class: 我的表格类:

def _article_form_widget():
    return forms.Textarea(
        attrs={'rows': 30}
        )

# [...] Some other forms

class NewArticleForm(forms.Form):

    """New article form"""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_id  = 'new_article_form'

        self.helper.add_input(Submit('submit', 'Submit'))

    name = forms.CharField(label='Article name', max_length=1100)
    slug = forms.SlugField(label='Article slug', max_length=1100)
    body = forms.CharField(label='Article body', widget=_article_form_widget())

My view: 我的观点:

class NewArticleView(FormView):

    form_class = NewArticleForm
    template_name = 'wiki/new_article.html'

    def form_valid(self, form):
        with transaction.atomic():
            self.article = Article(body=form.cleaned_data['body'])
            self.article.save()
            self.main_alias = Alias(
                name=form.cleaned_data['name'],
                slug=form.cleaned_data['slug'],
                article=self.article
            )
            self.main_alias.save()
        return super().form_valid(form)

    def get_success_url(self):
        return reverse_lazy('article-detail',
                            kwargs={'slug': self.main_alias.slug})

The template: 模板:

{% extends "base.html" %}

{% block content %}
    <h1>New article</h1>

    {% load crispy_forms_tags %}
    {% crispy form %}
{% endblock content %}

I tried updating Django and django-crispy-forms, but no avail. 我尝试更新Django和django-crispy-forms,但没有用。 Please help me solve this mystery. 请帮我解开这个谜。

There is nothing wrong here. 这里没有错。 The crispy templates look for a tag context variable which may or may not exist, and render stuff accordingly. crispy模板查找可能存在或不存在的tag上下文变量,并相应地呈现内容。

The reason you are seeing all those errors is because you have configured DEBUG level logging. 您看到所有这些错误的原因是您已配置DEBUG级别日志记录。 When this is done for templates , the docs say: 模板执行此操作时,文档会说:

Missing context variables are logged as DEBUG messages. 丢失的上下文变量记录为DEBUG消息。

The errors are nothing to worry about and it's working as it should. 这些错误无需担心,而且它的工作正常。

Looking at your Github repo it seems like you might have moved on already! 看看你的Github回购,看起来你可能已经开始了!

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

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