Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for request.GET variable in the template

I want to display a certain in the template only if a certain GET variable is set....I thought using {% if request.get.my_var %} would work, but it's not giving me the results.

like image 562
Stephen Avatar asked Jun 05 '10 17:06

Stephen


People also ask

How do I use a variable in a template?

In the template, you use the hash symbol, # , to declare a template variable. The following template variable, #phone , declares a phone variable with the <input> element as its value. Refer to a template variable anywhere in the component's template.

What is request Meta get?

get notation is to specify a default value of no value is present. E.g.: request. META. get("HTTP_REFERER", "localhost") would cause it to either return the actual value of HTTP_REFERER or return localhost if there is no HTTP_REFERER.


2 Answers

Variables are case-sensitive - so, assuming as lazerscience points out that you actually have the request object in the context, you would need to use {% if request.GET.my_var %}.

like image 110
Daniel Roseman Avatar answered Oct 17 '22 08:10

Daniel Roseman


Check if you have django.core.context_processors.request in your TEMPLATE_CONTEXT_PROCESSORS in settings.py.

If not put it there, or add request yourself to your rendered context.

http://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-request

like image 36
Bernhard Vallant Avatar answered Oct 17 '22 08:10

Bernhard Vallant