簡體   English   中英

帶有Django后端自動登錄會話的Android應用程序前端

[英]Android app front-end with Django back-end auto login session

我將Android應用程序前端與Django后端一起使用。 但是它沒有自動登錄。

from django.contrib.auth import login as auth_login
def login_app(request):
    ...
    if request.user.is_authenticated():
        ...
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    user = authenticate(username=username, password=password)
    if user is not None and user.is_active:
        auth_login(request, user)
        ...
    return ...

當我兩次調用此函子時。 它獲得一個匿名用戶實例。

if request.user.is_authenticated():

我該怎么做才能為真?
設置cookie? 怎么樣?
不好意思的英語借口。

if request.user.is_authenticated():

要點:請求用戶登錄成功。 例如,人們的cookie沒有過期。

auth_login執行成功時,django將幫助您保存用戶cookie和會話。

示例:在模板中:

{% if user.is_authenticated %}
    <p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
    <p>Welcome, new user. Please <a href={% url 'login_url' %}>log in</a>.</p>
{% endif %}

在視圖中:

def my_view(request):
   if not request.user.is_authenticated():
        return redirect('/login/')

在您的登錄視圖中:

def login_app(request):
    ...
    if request.user.is_authenticated():
        #tell user he has login successful,or redict other page
    else:
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        user = authenticate(username=username, password=password)
        if user is not None and user.is_active:
            auth_login(request, user)
        #tell user he login successfull

更多信息文檔

您的android應用已取消http響應,其中包含cookie信息,請保存並在下一個請求中攜帶cookie。

django的文件非常++,請仔細閱讀

不好意思的英語借口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM