2012-09-16 47 views
0

每當我重新加載我的頁面時,單擊瀏覽器的地址欄URL中的輸入或在另一個選項卡中打開相同的URL,會話似乎已過期。我的意思是我的頁面正在導航到登錄頁面。會話在django驗證頁中重新加載頁面時即將過期

這是我的看法。下面的視圖將在一個index.html HTML頁面中呈現。無論何時顯示用戶登錄的用戶名/密碼登錄表單,它都表示感謝您的登錄。所以這個功能工作正常。

def index(request): 
    if request.user.is_authenticated(): 
      return HttpResponseRedirect('/myapp/') 

    if request.method == 'POST': 
      form = UserLoginForm(request.POST) 
      if form.is_valid(): 
       username = form.cleaned_data['username'] 
       password = form.cleaned_data['password'] 
      if request.user.is_authenticated(): 
       return HttpResponseRedirect('/myapp/') 
      else: 
       user = authenticate(username = username, password = password) 
      return shortcuts.render_to_response('index.html',locals(), 
            context_instance = context.RequestContext(request)) 
    else: 
      form = UserLoginForm 

return shortcuts.render_to_response('index.html',locals(), 
            context_instance = context.RequestContext(request)) 

爲了您的參考,我在我的應用程序中安裝了以下中間件類。

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 

有人能幫助我在此請

-RAM

回答

1

添加對login函數的調用,因爲此函數負責保存用戶的ID在會議上,像這樣:

+0

現在我的瀏覽器說,這樣此網頁有重定向循環 在http網頁://本地主機:8090/MyApp的/已經導致了過多的重定向。清除您的本網站的Cookie或允許第三方Cookie可以解決此問題。如果沒有,這可能是一個服務器配置問題,而不是您的計算機的問題。我已經清除了所有的cookies並重新加載了,但是上面的消息仍在進行中 – vkrams

+0

沒關係,我自己通過在返回shortcuts.render_to_response('index.html')之後將返回的HttpResponseRedirect('/ myapp /')替換爲is_authenticated locals(),context_instance = context.RequestContext(request)) – vkrams