Display query string values in django templates

Question:

I wanted to print some success messages from get method back to the index page(home.html) i.e template page with the help of query string. I have redirected to the index page using

return HttpResponseRedirect("/mysite/q="+successfailure)

Now i wanted to print the string success along with other contents in the index file( or template file / home.html file).

I have searched for the solution and found that " django.core.context_processors.request context " should be added to the settings. But i did not find the place to add it. I am currently using python 2.7 and django 1.4.3.

I also tried to use

render_to_response("home.html",{'q':successfailure})

However, the result is printed in the current page(addContent.html- which I dont want) but i want to send the url into ‘/mysite/’ and print the result there.

Please suggest the appropriate solution. Thanks in advance.

Asked By: user2081099

||

Answers:

I’m not sure I understand the question, but you can access the querystring in your view with request.GET.

So you could adjust the view that renders home.html by adding the successfailure variable into the context. Something like –

def home_view(request):
    #....
    successfailure = request.GET
    return render(request, 'home.html', {'succsessfailure': successfailure.iteritems()})

Then iterate throught the variables in your template

{% for key, value in successfailure %}
    <p>{{ key }} {{ value }}</p>
{% endfor %}

If there’s a specific key in the querystring you can get the value with request.GET['your_key'] and drop the call to iteritems() (along with the iteration in the template).

Answered By: Aidan Ewen

these are the default context processors:
https://docs.djangoproject.com/en/dev/ref/templates/api/#using-requestcontext

TEMPLATES = [ {"OPTIONS": { "context_processors": [
    'django.template.context_processors.debug',
    'django.template.context_processors.request',
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
]}}]

if its not in your settings than you haven’t overridden it yet. do that now.

then in your template, something like this:

{% if request.GET.q %}<div>{{ request.GET.q }}</div>{% endif %}

also, I’m noticing in your link url you are not using a querystring operator, ?. You should be:

return HttpResponseRedirect("/mysite/?q="+successfailure)
Answered By: Francis Yaconiello

To Django Templates, you better redirect with post method as shown below because it’s easier and more secure:

# "views.py"

from django.shortcuts import redirect

def my_view(request): 
            # Here
    request.session["messages"] = {"success": "Success", "fail": "Fail"}
    return redirect('/mysite')
# "home.html"

{{ request.session.messages.success }} {# Success #}
{{ request.session.messages.fail }}    {# Fail #}

In addition, to Django Templates, you can also redirect with the several types of messages as shown below:

# "views.py"

from django.contrib import messages # Here
from django.shortcuts import redirect

def my_view(request): 
    messages.debug(request, 'This is debug')
    messages.info(request, 'This is info')
    messages.success(request, 'This is success')
    messages.warning(request, 'This is warning')
    messages.error(request, 'This is error')
    return redirect("/mysite")
# "home.html"

{% load i18n static %}<!DOCTYPE html>
{% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %}
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}">

</head>
{% load i18n %}

<body class="{% if is_popup %}popup {% endif %}{% block bodyclass %}{% endblock %}"
  data-admin-utc-offset="{% now "Z" %}">

{% if messages %}
<ul class="messagelist">{% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
{% endfor %}</ul>
{% endif %}
</body>
</html>

But, I don’t know why only "debug" message is not displayed even though "DEBUG = True" in "settings.py":

enter image description here

Answered By: Kai – Kazuya Ito