Display query string values ββin django templates
I wanted to print some success messages from the get method back to the index (home.html) ie template page using a query string. I have redirected the index page with
return HttpResponseRedirect("/mysite/q="+successfailure)
Now I wanted to print the success of the line along with other content in the index file (or the template file /home.html file).
I was looking for a solution and found that "django.core.context_processors.request context" should be added to the settings. But I couldn't find a place to add it. I am currently using python 2.7 and django 1.4.3.
I have also tried using
render_to_response("home.html",{'q':successfailure})
however the result is printed on the current page (addContent.html- which I don't want), but I want to send url to '/ mysite /' and print the result there.
Please suggest a suitable solution. Thanks in advance.
source to share
these are the default context processors: https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
TEMPLATE_CONTEXT_PROCESSORS = (
"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",
#add this
"django.core.context_processors.request"
)
if it is not in your settings than you have not overridden yet. do it now.
then in your template, something like this:
{% if request.GET.q %}<div>{{ request.GET.q }}</div>{% endif %}
also, im noticing in your link url that you are not using the querystring statement ?
, it should be:
return HttpResponseRedirect("/mysite/?q="+successfailure)
source to share
I'm not sure I understand this question, but you can access the querystring in your view using request.GET
.
So, you can customize the view that displays home.html by adding the successfailure variable to the context. Something like -
def home_view(request):
#....
successfailure = request.GET
return render(request, 'home.html', {'succsessfailure': successfailure.iteritems()})
Then iterate over the variables in your template
{% for key, value in successfailure %}
<p>{{ key }} {{ value }}</p>
{% endfor %}
If there is a specific key in the request, you can get the value with request.GET['your_key']
and drop the call to iteritems()
(along with the iteration in the template).
source to share