2011-05-24 120 views
0

這看起來像是一個非常簡單的問題,但我無法使其工作。如果@login_required裝飾器出現在特定的視圖函數中,則django註冊將傳入正確的?next=.....。一旦用戶登錄,用戶將被重定向到他想查看的頁面。這很好,工作正常。登錄後無法將django註冊重定向到?下一個

我的問題是我有這個html頁面,不需要登錄,但如果你想登錄,我有一個<a href='{% url auth_login>目前。如果用戶從此html登錄,則?next=會顯示一個空白並被重定向到默認位置accounts/profile/。我還沒有設置用戶配置文件,所以我得到一個TemplateDoesNotExist錯誤。

我有這樣的事情:

views.py 

... 
def display_all(request): 
    response = list_detail.object_list(
       request, 
       queryset = MyModel.objects.all(), 
       template_name = 'show_all.html' 
    ) 
    return response 

-

show_all.html 

<html> 
<head> 
      <title> Display Everything </title> 
</head> 
<body> 
      <a href='{% url auth_login %}?next={{ request.path }}'>Login</a> 
      <h1> Displaying all items! </h1> 
      <ul> 
        {% for item in object_list %} 
         <li> {{ item.name }} </li> 
        {% endfor %} 
      </ul> 
</body> 
</html> 

在這裏,我通過列舉出來展示的一切,我在頂部的登錄鏈接,如果你想登錄。 {{ request.path }}返回一個空字符串,所以我得到的最終結果是http://localhost:8000/accounts/login/?next=,它將重定向到默認accounts/profile/

回答

4

如果您的問題是request.path爲空,則需要在settings.py中將TEMPLATE_CONTEXT_PROCESSORS添加到「django.core.context_processors.request」中。 看django docs