最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

django用戶注冊、登錄、注銷和用戶擴展的示例

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

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

首先我假設(shè)你對django的session、cookie和數(shù)據(jù)庫、admin部分都有一定的了解,不了解的可以參考這個教程: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框架驗證用戶名和密碼,有人會說,這樣太不靈活了,我想用郵箱登錄呢?后面我們會說直接用django.contrib.auth.models.User 模型來直接操作用戶數(shù)據(jù),這樣就可以做自己想要的驗證了。
b、用戶信息被驗證無誤后需要把用戶登錄的信息寫入session中

2、用戶注銷

注銷比較簡單,只需要在session中刪除對應(yīng)的user信息就ok了

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

3、用戶注冊

注冊的界面如下:

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

注冊的視圖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() 
      #用戶擴展信息 profile 
      profile=UserProfile()#e************************* 
      profile.user_id=user.id 
      profile.phone=phone 
      profile.save() 
      #登錄前需要先驗證 
      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、驗證用戶是否登錄了,已經(jīng)登錄就沒必要注冊了(當(dāng)然這只是練習(xí)使用,實際生產(chǎn)情況可能不一樣)

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

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

d、直接利用User模型把通過驗證的用戶數(shù)據(jù)存入數(shù)據(jù)庫,需要注意的是,保存密碼信息時需要使用set_password方法(因為這里有個加密的過程)

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

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

g、用戶登錄

4、用戶擴展

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

A、在你App的models中新建一個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其實就是一個普通的model,然后通過這一句與django的User模型建立聯(lián)系

     b、擴展的用戶信息

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

C、如何調(diào)用user的擴展信息呢?很簡單,先得到user,然后通過user提供的get_profile()來得到profile對象,比如

user.get_profile().phone

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

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

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

很簡單,只要在你的APP的admin.py中添加下面的語句就行了

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時的一些經(jīng)驗,也許不全對,僅供參考,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python辦公自動化從Excel中計算整理數(shù)據(jù)并寫入Word

    Python辦公自動化從Excel中計算整理數(shù)據(jù)并寫入Word

    這篇文章主要為大家介紹了Python辦公自動化從Excel中計算整理數(shù)據(jù)并寫入Word示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Python實現(xiàn)自動駕駛訓(xùn)練模型

    Python實現(xiàn)自動駕駛訓(xùn)練模型

    這篇文章主要為大家介紹了Python實現(xiàn)自動駕駛訓(xùn)練模型,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • python密碼學(xué)實現(xiàn)文件加密教程

    python密碼學(xué)實現(xiàn)文件加密教程

    這篇文章主要為大家介紹了python密碼學(xué)實現(xiàn)文件加密教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • python使用pandas按照行數(shù)分割表格

    python使用pandas按照行數(shù)分割表格

    本文主要介紹了python使用pandas按照行數(shù)分割表格,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 詳解Python二維數(shù)組與三維數(shù)組切片的方法

    詳解Python二維數(shù)組與三維數(shù)組切片的方法

    這篇文章主要介紹了詳解Python二維數(shù)組與三維數(shù)組切片的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • python?argparse的使用步驟(全網(wǎng)最全)

    python?argparse的使用步驟(全網(wǎng)最全)

    argparse是python的一個命令行參數(shù)解析包,在代碼需要頻繁修改參數(shù)時,方便使用,主要用法就是在命令行輸入自己想要修改的參數(shù),這篇文章主要介紹了python?argparse的使用步驟(全網(wǎng)最全),需要的朋友可以參考下
    2023-04-04
  • 簡單總結(jié)Python中序列與字典的相同和不同之處

    簡單總結(jié)Python中序列與字典的相同和不同之處

    這篇文章主要介紹了Python中序列與字典的相同和不同之處,序列這里講到Python中最常用的列表和元組以及字典三種,需要的朋友可以參考下
    2016-01-01
  • python執(zhí)行等待程序直到第二天零點的方法

    python執(zhí)行等待程序直到第二天零點的方法

    這篇文章主要介紹了python執(zhí)行等待程序直到第二天零點的方法,涉及Python等待程序的實現(xiàn)技巧,需要的朋友可以參考下
    2015-04-04
  • Python中python-nmap模塊的使用介紹

    Python中python-nmap模塊的使用介紹

    這篇文章主要介紹了Python中python-nmap模塊的使用,主要是portScanner()類方法展開全文,portScanner()類用于實現(xiàn)對指定主機進(jìn)行端口掃描,更多介紹內(nèi)容,需要的朋友可以參考一下
    2022-02-02
  • Python Pandas常用函數(shù)方法總結(jié)

    Python Pandas常用函數(shù)方法總結(jié)

    今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識,文章圍繞著Pandas常用函數(shù)方法展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06

最新評論

开阳县| 镇赉县| 马边| 河津市| 台东市| 拉孜县| 宝山区| 砚山县| 乳山市| 稷山县| 新泰市| 三河市| 利辛县| 西吉县| 犍为县| 营口市| 德安县| 阿合奇县| 从化市| 乐亭县| 黑龙江省| 阳西县| 华池县| 蒲江县| 仙桃市| 呼玛县| 济阳县| 鄂托克前旗| 普兰县| 漳平市| 肥乡县| 资源县| 奉化市| 汉中市| 阿鲁科尔沁旗| 泰兴市| 天柱县| 葫芦岛市| 石渠县| 奎屯市| 西乌珠穆沁旗|