Django,如何将数据对象从一个模板传递到另一个模板

Nir*_*pla 2 python django templates django-views

我在视图中有一个数据对象列表,并将其传递给模板以汇总列表.现在,从该模板我想将数据对象带到下一页(模板)以显示数据对象的详细信息.

在我的view.py中

schools = PublicSchools.objects.all()
return render_to_response('searchresult.html', {'schools': schools}, context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)

在我的模板searchresult.html中,我列出了学校的摘要数据.

反过来,我想将单个学校对象发送到下一个模板(dateils.html)以显示特定学校的详细信息.

有人可以帮忙吗?

谢谢,

kar*_*eek 5

您可以编写另一个视图来处理您的需求

试试这个示例代码

网址

# pass the selected school id form template to your view   
url(r'^school/(?P<school_id>\d+)/$', schoolDetails), 
Run Code Online (Sandbox Code Playgroud)

意见

def schoolDetails(request, school_id):
    try:
        school = PublicSchools.objects.get(pk=school_id)
    except school.DoesNotExist:
        raise Http404
    return render(request, 'detail.html', {'school': school})
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助你:)