欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

django用戶注冊(cè)、登錄、注銷(xiāo)和用戶擴(kuò)展的示例

 更新時(shí)間:2018年03月19日 11:13:07   作者:cclehui  
本篇文章主要介紹了django用戶注冊(cè)、登錄、注銷(xiāo)和用戶擴(kuò)展的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

用戶部分是一個(gè)網(wǎng)站的基本功能,django對(duì)這部分進(jìn)行了很好的封裝,我們只需要在django的基礎(chǔ)上做些簡(jiǎn)單的修改就可以達(dá)到我們想要的效果

首先我假設(shè)你對(duì)django的session、cookie和數(shù)據(jù)庫(kù)、admin部分都有一定的了解,不了解的可以參考這個(gè)教程:http://djangobook.py3k.cn/2.0/

1、用戶登錄:

首先假設(shè)有這樣的登錄界面:

處理登錄的視圖代碼如下:

def userLogin(request): 
  curtime=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()); 
     
  if request.method=='POST': 
    print("POST") 
    username=request.POST.get('name','') 
    password=request.POST.get('password','') 
    user= auth.authenticate(username=username,password=password)#a*********** 
    if user and user.is_active: 
      auth.login(request, user)#b************ 
      return HttpResponseRedirect("/user") 
        
  return render_to_response("blog/userlogin.html",RequestContext(request,{'curtime':curtime}))  

注:a、這里是用django自己的auth框架驗(yàn)證用戶名和密碼,有人會(huì)說(shuō),這樣太不靈活了,我想用郵箱登錄呢?后面我們會(huì)說(shuō)直接用django.contrib.auth.models.User 模型來(lái)直接操作用戶數(shù)據(jù),這樣就可以做自己想要的驗(yàn)證了。
b、用戶信息被驗(yàn)證無(wú)誤后需要把用戶登錄的信息寫(xiě)入session中

2、用戶注銷(xiāo)

注銷(xiāo)比較簡(jiǎn)單,只需要在session中刪除對(duì)應(yīng)的user信息就ok了

def userLogout(request): 
  auth.logout(request) 
  return HttpResponseRedirect('/user') 

3、用戶注冊(cè)

注冊(cè)的界面如下:

用戶名、密碼、郵箱是基本的注冊(cè)信息,這是django自帶的,下面的電話是擴(kuò)展的用戶信息,至于這么擴(kuò)展用戶信息,一會(huì)會(huì)講,先透露下我采用的是profile的擴(kuò)展方式(個(gè)人喜好吧,我覺(jué)得這種方式簡(jiǎn)單明了)

注冊(cè)的視圖view代碼:

def userRegister(request): 
  curtime=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()); 
   
  if request.user.is_authenticated():#a******************* 
    return HttpResponseRedirect("/user") 
  try: 
    if request.method=='POST': 
      username=request.POST.get('name','') 
      password1=request.POST.get('password1','') 
      password2=request.POST.get('password2','') 
      email=request.POST.get('email','') 
      phone=request.POST.get('phone','') 
      errors=[] 
       
      registerForm=RegisterForm({'username':username,'password1':password1,'password2':password2,'email':email})#b******** 
      if not registerForm.is_valid(): 
        errors.extend(registerForm.errors.values()) 
        return render_to_response("blog/userregister.html",RequestContext(request,{'curtime':curtime,'username':username,'email':email,'errors':errors})) 
      if password1!=password2: 
        errors.append("兩次輸入的密碼不一致!") 
        return render_to_response("blog/userregister.html",RequestContext(request,{'curtime':curtime,'username':username,'email':email,'errors':errors})) 
         
      filterResult=User.objects.filter(username=username)#c************ 
      if len(filterResult)>0: 
        errors.append("用戶名已存在") 
        return render_to_response("blog/userregister.html",RequestContext(request,{'curtime':curtime,'username':username,'email':email,'errors':errors})) 
       
      user=User()#d************************ 
      user.username=username 
      user.set_password(password1) 
      user.email=email 
      user.save() 
      #用戶擴(kuò)展信息 profile 
      profile=UserProfile()#e************************* 
      profile.user_id=user.id 
      profile.phone=phone 
      profile.save() 
      #登錄前需要先驗(yàn)證 
      newUser=auth.authenticate(username=username,password=password1)#f*************** 
      if newUser is not None: 
        auth.login(request, newUser)#g******************* 
        return HttpResponseRedirect("/user") 
  except Exception,e: 
    errors.append(str(e)) 
    return render_to_response("blog/userregister.html",RequestContext(request,{'curtime':curtime,'username':username,'email':email,'errors':errors})) 
   
  return render_to_response("blog/userregister.html",RequestContext(request,{'curtime':curtime})) 

注:

a、驗(yàn)證用戶是否登錄了,已經(jīng)登錄就沒(méi)必要注冊(cè)了(當(dāng)然這只是練習(xí)使用,實(shí)際生產(chǎn)情況可能不一樣)

b、注冊(cè)表單傳過(guò)來(lái)的數(shù)據(jù)需要一些基本的驗(yàn)證,怎么驗(yàn)證表單數(shù)據(jù)可以參考這個(gè)教程:http://djangobook.py3k.cn/2.0/chapter07/

c、用User模型查找要注冊(cè)的用戶名是否存在,如果用戶已經(jīng)存在就需要提示注冊(cè)的客戶更換用戶名

d、直接利用User模型把通過(guò)驗(yàn)證的用戶數(shù)據(jù)存入數(shù)據(jù)庫(kù),需要注意的是,保存密碼信息時(shí)需要使用set_password方法(因?yàn)檫@里有個(gè)加密的過(guò)程)

e、存儲(chǔ)用戶的擴(kuò)展信息(這里是用戶的電話號(hào)碼),這里用到自定義的用戶擴(kuò)展模型UserProfile,具體怎么擴(kuò)展用戶后面會(huì)講

f、用戶登錄前需要先進(jìn)行驗(yàn)證,要不然會(huì)出錯(cuò)

g、用戶登錄

4、用戶擴(kuò)展

網(wǎng)上關(guān)于django的用戶擴(kuò)展方式有好幾種,個(gè)人比較傾向于Profile的方式,主要是這種方式簡(jiǎn)單清楚,擴(kuò)展步驟如下:

A、在你App的models中新建一個(gè)UserProfile模型

from django.contrib.auth.models import User 
     
class UserProfile(models.Model): 
  user=models.OneToOneField(User,unique=True,verbose_name=('用戶'))#a****** 
  phone=models.CharField(max_length=20)#b****** 

注:a、UserProfile其實(shí)就是一個(gè)普通的model,然后通過(guò)這一句與django的User模型建立聯(lián)系

     b、擴(kuò)展的用戶信息

B、python manage.py syncdb 在數(shù)據(jù)庫(kù)內(nèi)創(chuàng)建userprofile的表

C、如何調(diào)用user的擴(kuò)展信息呢?很簡(jiǎn)單,先得到user,然后通過(guò)user提供的get_profile()來(lái)得到profile對(duì)象,比如

user.get_profile().phone

D、如何更新和存儲(chǔ)user的profile信息呢,其實(shí)在之前的用戶注冊(cè)部分我們已經(jīng)使用了這樣的功能,userprofile其實(shí)也是一個(gè)model,我們只要通過(guò)user模型得到user的id,就可以通過(guò)UserProfile模型來(lái)操作對(duì)應(yīng)的profile信息:

user=User() 
user.username=username 
user.set_password(password1) 
user.email=email 
user.save() 
#用戶擴(kuò)展信息 profile 
profile=UserProfile() 
profile.user_id=user.id 
profile.phone=phone 
profile.save() 

E、我們能在程序中操作用戶擴(kuò)展信息了,那我想在admin后臺(tái)中編輯擴(kuò)展信息要怎么做呢:

很簡(jiǎn)單,只要在你的APP的admin.py中添加下面的語(yǔ)句就行了

class UserProfileInline(admin.StackedInline): 
  model=UserProfile 
  fk_name='user' 
  max_num=1 
   
class UserProfileAdmin(UserAdmin): 
  inlines = [UserProfileInline, ] 
   
admin.site.unregister(User) 
admin.site.register(User,UserProfileAdmin) 

這是我學(xué)習(xí)django時(shí)的一些經(jīng)驗(yàn),也許不全對(duì),僅供參考,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Python調(diào)用華為API實(shí)現(xiàn)圖像標(biāo)簽

    詳解Python調(diào)用華為API實(shí)現(xiàn)圖像標(biāo)簽

    華為云圖像標(biāo)簽可識(shí)別上千種通用物體以及數(shù)百種場(chǎng)景標(biāo)簽,一個(gè)圖像可包含多個(gè)標(biāo)簽內(nèi)容,語(yǔ)義內(nèi)容非常豐富。本文將通過(guò)Python調(diào)用華為API實(shí)現(xiàn)圖像標(biāo)簽,需要的可以參考一下
    2022-04-04
  • Jinja2實(shí)現(xiàn)模板渲染與訪問(wèn)對(duì)象屬性流程詳解

    Jinja2實(shí)現(xiàn)模板渲染與訪問(wèn)對(duì)象屬性流程詳解

    要了解jinja2,那么需要先理解模板的概念。模板在Python的web開(kāi)發(fā)中廣泛使用,它能夠有效的將業(yè)務(wù)邏輯和頁(yè)面邏輯分開(kāi),使代碼可讀性增強(qiáng),并且更加容易理解和維護(hù)。模板簡(jiǎn)單來(lái)說(shuō)就是一個(gè)其中包含占位變量表示動(dòng)態(tài)部分的文,模板文件在經(jīng)過(guò)動(dòng)態(tài)賦值后,返回給用戶
    2023-03-03
  • Python request post上傳文件常見(jiàn)要點(diǎn)

    Python request post上傳文件常見(jiàn)要點(diǎn)

    這篇文章主要介紹了Python request post上傳文件常見(jiàn)要點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • python基礎(chǔ) range的用法解析

    python基礎(chǔ) range的用法解析

    這篇文章主要介紹了python基礎(chǔ) range的用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python打包文件執(zhí)行報(bào)錯(cuò):ModuleNotFoundError: No module named ‘pymssql‘的解決方法

    Python打包文件執(zhí)行報(bào)錯(cuò):ModuleNotFoundError: No module 

    這篇文章給大家介紹了Python打包文件執(zhí)行報(bào)錯(cuò):ModuleNotFoundError: No module named ‘pymssql‘的解決方法,如果有遇到相同問(wèn)題的朋友可以參考閱讀一下本文
    2023-10-10
  • Python編程flask使用頁(yè)面模版的方法

    Python編程flask使用頁(yè)面模版的方法

    今天小編就為大家分享一篇關(guān)于Python編程flask使用頁(yè)面模版的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • Python : turtle色彩控制實(shí)例詳解

    Python : turtle色彩控制實(shí)例詳解

    今天小編就為大家分享一篇Python : turtle色彩控制實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • python中如何使用函數(shù)改變list

    python中如何使用函數(shù)改變list

    這篇文章主要介紹了python中如何使用函數(shù)改變list,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 以視頻爬取實(shí)例講解Python爬蟲(chóng)神器Beautiful Soup用法

    以視頻爬取實(shí)例講解Python爬蟲(chóng)神器Beautiful Soup用法

    這篇文章主要以視頻爬取實(shí)例來(lái)講解Python爬蟲(chóng)神器Beautiful Soup的用法,Beautiful Soup是一個(gè)為Python獲取數(shù)據(jù)而設(shè)計(jì)的包,簡(jiǎn)潔而強(qiáng)大,需要的朋友可以參考下
    2016-01-01
  • python類繼承與子類實(shí)例初始化用法分析

    python類繼承與子類實(shí)例初始化用法分析

    這篇文章主要介紹了python類繼承與子類實(shí)例初始化用法,實(shí)例分析了Python類的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04

最新評(píng)論