django寫用戶登錄判定并跳轉(zhuǎn)制定頁面的實(shí)例
1. 首先看要設(shè)置登陸的界面 book/view.py
@user_util.my_login #相當(dāng)于 select_all=my_login(select_all) def select_all(request): # 查詢所有的書 book_list = BookInfo.objects.all() # 返回 return render(request, 'book/book_list.html', {'book_list': book_list}) @user_util.my_login #相當(dāng)于 select_by_id=my_login(select_by_id) def select_by_id(request,book_id): bookinfo=BookInfo.objects.get(id=book_id) return render(request,'book/book_detail.html',{'bookinfo':bookinfo})
@user_util.my_login 是在utils/user_utils.py里面寫的裝飾器
使用session判定是否登錄:login_user_id = args[0].session.get(‘login_user_id')
#登陸用的裝飾器 def my_login(func): def inner(*args,**kwargs): login_user_id = args[0].session.get('login_user_id') if login_user_id: return func(*args,**kwargs) else: return redirect(reverse('user:login')) return inner
2. 設(shè)置登錄模塊的界面
設(shè)置session鍵值對進(jìn)行存儲: request.session[‘login_user_id']=user[0].id
def login(request): #獲取cookie remember_user_name=request.COOKIES.get('remember_user_name','') return render(request, "user/login.html",{'remember_user_name':remember_user_name}) def login_handler(request): # 判斷請求方式 if request.method == "GET": request_info = request.GET else: request_info = request.POST # 獲取屬性 user_name = request_info.get("user_name",'').strip() user_pwd = request_info.get("user_pwd",'').strip() remember = request_info.get("remember") user_pwd = my_md5(user_pwd) user=User.objects.filter(name=user_name, pwd=user_pwd) #print(user[0].id) # 查詢 if len(user) != 0: #將登陸信息保存到session request.session['login_user_id']=user[0].id resp=HttpResponseRedirect(reverse("book:index")) #記住用戶名 if remember=='1': resp.set_cookie('remember_user_name',user_name,3600*24*7) else: resp.set_cookie('remember_user_name',user_name,0) return resp # 轉(zhuǎn)發(fā) #return book_views.index(request) # 重定向 #return HttpResponseRedirect(reverse("book:index")) else: return HttpResponseRedirect(reverse("user:login"))
以上這篇django寫用戶登錄判定并跳轉(zhuǎn)制定頁面的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 如何基于Django實(shí)現(xiàn)上下文章跳轉(zhuǎn)
- 詳解django使用include無法跳轉(zhuǎn)的解決方法
- Django 在iframe里跳轉(zhuǎn)頂層url的例子
- django創(chuàng)建最簡單HTML頁面跳轉(zhuǎn)方法
- 在django中實(shí)現(xiàn)頁面倒數(shù)幾秒后自動跳轉(zhuǎn)的例子
- django 控制頁面跳轉(zhuǎn)的例子
- Django框架之登錄后自定義跳轉(zhuǎn)頁面的實(shí)現(xiàn)方法
- django頁面跳轉(zhuǎn)問題及注意事項(xiàng)
- Django實(shí)現(xiàn)文章詳情頁面跳轉(zhuǎn)代碼實(shí)例
相關(guān)文章
python實(shí)現(xiàn)JAVA源代碼從ANSI到UTF-8的批量轉(zhuǎn)換方法
這篇文章主要介紹了python實(shí)現(xiàn)JAVA源代碼從ANSI到UTF-8的批量轉(zhuǎn)換方法,涉及Python針對文件操作與編碼轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-08-08Python函數(shù)參數(shù)分類使用與新特性詳細(xì)分析講解
在聲明函數(shù)的時候,一般會根據(jù)函數(shù)所要實(shí)現(xiàn)的功能來決定函數(shù)是否需要參數(shù)。在多數(shù)情況下,我們聲明的函數(shù)都會使用到參數(shù),這篇文章主要介紹了Python函數(shù)參數(shù)2023-01-01Python中dtype、type()和astype()的區(qū)別詳解
這篇文章主要介紹了Python中dtype、type()和astype()的區(qū)別詳解,type()是python內(nèi)置的函數(shù),type()返回數(shù)據(jù)結(jié)構(gòu)類型(list、dict、numpy.ndarray 等),需要的朋友可以參考下2023-08-08在PyCharm導(dǎo)航區(qū)中打開多個Project的關(guān)閉方法
今天小編就為大家分享一篇在PyCharm導(dǎo)航區(qū)中打開多個Project的關(guān)閉方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01快速了解Python開發(fā)中的cookie及簡單代碼示例
這篇文章主要介紹了快速了解Python開發(fā)中的cookie及簡單代碼示例,具有一定借鑒價值,需要的朋友可以參考下2018-01-01解決pyCharm中 module 調(diào)用失敗的問題
今天小編就為大家分享一篇解決pyCharm中 module 調(diào)用失敗的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02