Django从另一个视图调用视图

时间:2013-10-16 07:55:29

标签: python django django-views

所以我有一个视图,显示一些基于从主页搜索的Person的数据:

def film_chart_view(request):
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
        # grab the first person on the list
        try:
            person_search = Person.objects.filter(short = q)[0]
            filminfo = filmInfo(person_search.film_set.all())
            film_graph_data = person_search.film_set.all().order_by('date')
        #Step 1: Create a DataPool
            return render_to_response('home/search_results.html',{'query': q, 'high': filminfo[0],
              'graph_data': film_graph_data}, RequestContext(request))
        except IndexError:
            return render_to_response('home/not_found.html',{'query': q}, RequestContext(request))

在主页上,我还想要一个随机按钮,显示数据库中随机人员的一些数据,并使用上面的视图显示它。到目前为止,我有这样的观点:

def random_person(request):
    # 1282302 is max number of people currently
    get_random = random.randint(1,1282302)
    get_person = Person.objects.get(pk=get_random)
    person_name = get_person.full

但我不确定如何完成它,因此重定向到film_chart_view。

1 个答案:

答案 0 :(得分:1)

您可以从随机视图相应的网址重定向到指定的视图

def random_person(request):
    # 1282302 is max number of people currently
    get_random = random.randint(1,1282302)
    get_person = Person.objects.get(pk=get_random)
    person_name = get_person.full
    return HttpResponseRedirect(reverse('film_chart_view')+"?q="+get_person.short)