[Answer]-Displaying view variables in template with Django?

1👍

✅

Build a result dictionary in your view as:

result = {}
if search.is_valid():
    success = True
    subject = search.cleaned_data['search']
    sourceCode = urllib2.urlopen("http://finance.yahoo.com/q/ks?s="+subject).read()
    pbr = sourceCode.split('Price/Book (mrq):</td><td class="yfnc_tabledata1">')[1].split('</td>')[0]
    result['pbr'] = pbr
    result['search'] = search

and return this result as:

return render_to_response('ui/search.html', {"result":result},  context)

In your template now you can access the pbr as:

<h2>{{ result.search.value|upper }}</h2>
<h2>{{ result.pbr }}</h2>

Leave a comment