2015-01-14 42 views
0

如果未創建會話,我想重定向到django中的另一個url。 這是我在view.py中的函數代碼。它跳過重定向('/ login')行。我想暫時重定向到網址,以便在登錄後返回到相同的功能。無法重定向到django python中的另一個url

def myfunction(request): 
    if request.method == "GET": 
     client_id=request.GET['client_id'] 
     if 'member_id' not in request.session: 
      redirect('/login') 
     print "hello" 
     #print request.session['member_id'] 
     m=client.objects.get(id=client_id) 
    return HttpResponse("Done") 

回答

3

您需要返回redirect功能:

return redirect('/login')應該工作。

+0

謝謝!但我面臨着另一個問題。如果會話未創建,則轉到登錄頁面並且用戶登錄並創建會話。但是在這種情況下,我必須返回HttpResponse對象,並且它不會恢復到之前離開的位置 –

2

根據該文檔,該行應爲:

if 'member_id' not in request.session: 
    return redirect('/login') 

文檔:https://docs.djangoproject.com/en/1.7/topics/http/shortcuts/#django.shortcuts.redirect

然而,您的具體說法是該行並沒有執行。如果是這種情況,則在if聲明前打印reqeust.session以查看'member_id'是否在其中。

您還在redirect行有一個小的格式錯誤(沒有足夠的空格)。

+0

這應該是接受的答案,因爲它回答了'它跳過重定向('/ login')行.'部分的問題。 – That1Guy

相關問題