I want to be able to access a bit of information that someone places in a GET variable right in the template of a page (HTML escaped, of course.) How would I go about doing this? I know you can grab this information with views, but in this case, I'd rather handle it HTML side.
You can pass that information from view to the template just as passing another variable. When you are rendering your template, just add a variable and pass request.GET
QueryDict. You will be able to access all of the GET parameters within your template.
EDIT
direct_to_template
automatically includes RequestContext(request)
so you will be able to use all of your context instances in your settings. Please add 'django.core.context_processors.request'
in your TEMPLATE_CONTEXT_PROCESSORS
in settings.py. Afterwards, you will be able to use access Django's HttpRequest by {{ request }}
in your template. Example settings, urls, and template are below:
settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
# these are the default values from django. I am not sure whether they
# are overritten when setting this variable, so I am including them "django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages"
)
urls.py
urlpatterns = patterns('django.views.generic.simple',
url(r'^about/$', 'direct_to_template', {'template':
'about.html'}),
)
about.html
Your request is: <br /><br />
{{ request.GET }}
Please also see the documentation about the topic:
https://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext
https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS
request.get
? Accordingly to django, direct_to_template
adds RequestContext by default, which should enable you to access request
parameters in your template. –
Jewfish 'django.contrib.auth.context_processors.auth'
to avoid the KeyError on the admin panel. –
Tamarisk In new Django you can use directly in template like:
if post request with request.POST['name'] in view.py, in template you can use:
{{request.POST.name}}
if get request in template you can use:
{{request.GET.name}}
For example, if you submit the GET
request values in index.html
as shown below:
{# "index.html" #}
<form action="{% url 'my_app1:test' %}" method="get">
<input type="text" name="fruits" value="apple" /></br>
<input type="text" name="meat" value="beef" /></br>
<input type="submit" />
</form>
Then, you can get the GET
request values in test.html
as shown below. *My answer explains how to get GET
request values in Django:
{# "test.html" #}
{{ request.GET.fruits }} {# apple #}
{{ request.GET.meat }} {# beef #}
{{ request.GET.dict }} {# {'fruits': 'apple', 'meat': 'beef'} #}
{{ request.META.QUERY_STRING }} {# fruits=apple&meat=beef #}
And, if you submit the POST
request values in index.html
as shown below. *method="post"
needs csrf_token tag in <form></form>
otherwise there is error:
{# "index.html" #}
<form action="{% url 'my_app1:test' %}" method="post">
{% csrf_token %}
<input type="text" name="fruits" value="apple" /></br>
<input type="text" name="meat" value="beef" /></br>
<input type="submit" />
</form>
Then, you can get the POST
request values in test.html
as shown below. *My answer explains how to get POST
request values in Django:
{# "test.html" #}
{{ request.POST.fruits }} {# apple #}
{{ request.POST.meat }} {# beef #}
{{ request.POST.dict }} {# {'csrfmiddlewaretoken': 'Vzjk89LPweM4loDWTb9gFNHlRQNJRMNwzQWsiUaWNhgBOr8aLfZyPjHobgqFJimk', 'fruits': 'apple', 'meat': 'beef'} #}
© 2022 - 2024 — McMap. All rights reserved.