Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display query string values in django templates

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.

like image 519
user2081099 Avatar asked Feb 17 '13 19:02

user2081099


1 Answers

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)
like image 54
Francis Yaconiello Avatar answered Oct 16 '22 05:10

Francis Yaconiello