Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. Please help me solve this mystery.

like image 920
art-solopov Avatar asked Mar 28 '16 23:03

art-solopov


1 Answers

There is nothing wrong here. The crispy templates look for a tag context variable which may or may not exist, and render stuff accordingly.

The reason you are seeing all those errors is because you have configured DEBUG level logging. When this is done for templates, the docs say:

Missing context variables are logged as DEBUG messages.

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!

like image 130
solarissmoke Avatar answered Nov 15 '22 02:11

solarissmoke