2012-08-23 43 views
1

我正在使用Django註冊。我做了所有的步驟在我的項目中包含應用程序。django註冊:如何驗證用戶?

用戶登錄成功後,我想在其他頁面上重定向他。 我做到了,但問題是,我不想檢查用戶是否已通過身份驗證或不在模板內。

因爲任何用戶都可以訪問此頁面,所以可以直接在Web瀏覽器上輸入。

謝謝

回答

3

你可以有一個look at the documentation

{% if user.is_authenticated %} 
    <p>Welcome, {{ user.username }}. Thanks for logging in.</p> 
{% else %} 
    <p>Welcome, new user. Please log in.</p> 
{% endif %} 
1

一個視圖中:

if request.user.is_authenticated(): 
    # User is logged in 

模板內:

{% if request.user.is_authenticated %} 
    Only rendered for users that are logged in. 
{% endif %} 
0

爲了防止nonauthenti cated用戶看到的網頁,你可以添加一個@login_required裝飾到視圖(doc):

from django.contrib.auth.decorators import login_required 

@login_required 
def my_view(request): 
    ... 

因此,如果用戶沒有登錄,裝飾重定向他settings.LOGIN_URL