[Fixed]-Display query string values in django templates


these are the default context processors:

TEMPLATES = [ {"OPTIONS": { "context_processors": [

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)


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).


To Django Templates, you better redirect with session using request.session["key"] as shown below because it’s easier:

# "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 %}>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}">

{% 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 %}

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

enter image description here

Leave a comment