dl8
dl8

Reputation: 1270

Django calling a view from another view

So I have a view that displays some data based on the Person that is searched from the home page:

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))

On the homepage I also want to have a random button that displays some data from a random person on the database and display it with the above view. So far I have this view:

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

but I'm not sure how to complete it so it redirects to the film_chart_view.

Upvotes: 0

Views: 5573

Answers (1)

Rohan
Rohan

Reputation: 53316

You can redirect from random view appropriate url to the specified view as

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)

Upvotes: 2

Related Questions