GingerNinja23
GingerNinja23

Reputation: 162

Passing values from views.py to a template

Maybe this is a very silly question, but I'm new to django. My application doesn't require the use of any database, so my models.py is empty.My application receives a POST request from a template which is handled in my views.py and the response is another template.How can I pass values from views.py to my new template (Without using the database)?

My views.py

from django.shortcuts import render_to_response
from django.template import RequestContext


def search_isbn(request):
    if request.method == 'POST':
        x=500
    return render_to_response('results.html',RequestContext(request))

I wish to pass the value x to results.html.How can I do that ? Any help would be appreciated. Thanks in Advance.

Upvotes: 0

Views: 2480

Answers (2)

Dmitry Astrikov
Dmitry Astrikov

Reputation: 657

Also you can use render function.

from django.shortcuts import render

def search_isbn(request):
    x = 0
    if request.method == 'POST':
        x=500
    return render('results.html', {'x':x})

Upvotes: 1

falsetru
falsetru

Reputation: 368894

Pass a dictionary that contains the variables you want to pass.

def search_isbn(request):
    x = 0 # Without this, you will get NameError if request.method is not 'POST'
    if request.method == 'POST':
        x = 500
    return render_to_response('results.html', RequestContext(request, {'x': x}))
    #                                                                 ^^^^^^^^

Upvotes: 1

Related Questions