SverkerSbrg
SverkerSbrg

Reputation: 503

Set template variable in form

Is it possible to set pass a variable to a template from a form.

Eg in its simplest form something like this:

forms.py

class Form1(forms.Form):
    email = forms.EmailField(label="Your email")
    name = forms.CharField(label="Name")
    message = forms.CharField(label="Body", widget=forms.Textarea)
    FORM_HEADER = "FOOO!"

views.py

def motor(request):
   return render(request, 'form.html', RequestContext(request, {'form': MotorQuoteRequestForm(),}))

and in form.html

...
{{ FORM_HEADER }}
<hr>
{% crispy form %}
....

So that you can create a neat little tag to import of the entire form package when you want to use it.

Help would be much appreciated :D

Upvotes: 0

Views: 209

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600051

Why not {{ form.FORM_HEADER }}?

Although it's probably simpler just to pass the header into the context as a separate variable.

Upvotes: 2

Related Questions