How to get GET and POST request values in Django Templates?
Asked Answered
A

3

8

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.

Applied answered 15/12, 2012 at 18:54 Comment(2)
Can you be a bit more clear. How do you want to handle the GET and POST in your template. I am asking this because GET and POST are methods through which variables are sent to views and within the template.Danidania
I'm using direct_to_template, and want to put one of the variables in a hidden value in a form.Applied
J
12

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

Jewfish answered 15/12, 2012 at 19:5 Comment(5)
My hope is to avoid using a view in this instance. Is there no way to grab the variables without passing them explicitly from a view? I'm using direct_to_template, and want to put one of the variables in a hidden value in a form.Applied
Have you tried in your view to access the information you want with request.get? Accordingly to django, direct_to_template adds RequestContext by default, which should enable you to access request parameters in your template.Jewfish
Could you give an example and place it in the answer? It sounds just like what I want.Applied
Here it is. Edited the answer. You can check it.Jewfish
You'll also need 'django.contrib.auth.context_processors.auth' to avoid the KeyError on the admin panel.Tamarisk
S
3

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}}
Scutum answered 24/2, 2021 at 1:2 Comment(0)
W
0

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'} #}
Weidman answered 19/9, 2023 at 21:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.