如何编写一个视图页面,显示使用django从python方法返回的列表

时间:2012-08-30 18:33:42

标签: python html django django-views

我在views.py文件中有这个方法,它打印从方法rangeOfList()返回的列表。
我想在html页面上显示它将浏览器重定向到(result.html)。 我该怎么写呢?

def contact(request):
if request.method == 'POST': # If the form has been submitted...
    form = ContactForm(request.POST) # A form bound to the POST data

    if form.is_valid():
        form = form.cleaned_data
        parameters = organize(form)
        listToPrint = rangeOfList(parameters['money'][2], parameters['money'][4], 'total_money')



    return HttpResponseRedirect('result') # Redirect after POST
else:
    form = ContactForm() # An unbound form

return render(request, 'crunchApp/contact.html', {
    'form': form,
})

1 个答案:

答案 0 :(得分:0)

您应该将其添加到session

request.session['the_list'] = listToPrint
return redirect('result/')

确保您已在模板中激活了request template context processor

{% for i in request.session.the_list %}
   {{ i }}
{% endfor %}