真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

PythonDjango怎么實(shí)現(xiàn)登錄注冊(cè)

本篇內(nèi)容介紹了“Python Django怎么實(shí)現(xiàn)登錄注冊(cè)”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)建站主要從事做網(wǎng)站、成都做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)榮縣,十余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220

1.modles層 與admin

點(diǎn)擊(此處)折疊或打開

  1. # -*- coding: utf-8 -*-

  2. from __future__ import unicode_literals

  3. from django.db import models

  4. from django.contrib.auth.models import AbstractUser

  5. from django.utils.encoding import python_2_unicode_compatible

  6. ###普通用戶的數(shù)據(jù)模型

  7. @python_2_unicode_compatible

  8. class NewUser(AbstractUser):

  9.     profile= models.CharField('profile', default='', max_length=256)

  10.     def __str__(self):

  11.         return self.username

  12. # Create your models here.



##admin
# -*- coding: utf-8 -*- 

from django.contrib import admin from .models import NewUser class NewUserAdmin(admin.ModelAdmin):
    list_display = ('username','date_joined', 'profile')



admin.site.register(NewUser,NewUserAdmin)


2.setting設(shè)置newuser管理系統(tǒng)賬號(hào)進(jìn)行驗(yàn)證

  1. AUTH_USER_MODEL = "pric.NewUser"

3  views視圖層

  1. # -*- coding: utf-8 -*-

  2. from django.shortcuts import render

  3. from .forms import *

  4. from .models import *

  5. from django.contrib.auth import authenticate, login, logout

  6. from django.http import HttpResponse

  7. from django.shortcuts import render, redirect,render_to_response

  8. from django.core.exceptions import ObjectDoesNotExist

  9. from django.contrib.auth.decorators import login_required

  10. @login_required

  11. def index(request):

  12.     return render_to_response('index.html', locals())

  13. ###登錄

  14. def log_in(request):

  15.     if request.method == 'GET':

  16.         form = LoginForm()

  17.         return render(request, 'login.html', {'form': form})

  18.     if request.method == 'POST': # 當(dāng)提交表單時(shí)

  19.         form = LoginForm(request.POST) # form 包含提交的數(shù)據(jù)

  20.         if form.is_valid(): # 如果提交的數(shù)據(jù)合法

  21.             username = form.cleaned_data['uid']

  22.             password = form.cleaned_data['pwd']

  23.             user = authenticate(username=username, password=password)  

  24.             if user is not None: ###如果用戶驗(yàn)證存在,

  25.                 login(request, user) #開始登錄

  26.                 return redirect('index') #跳轉(zhuǎn)首頁

  27.             else:

  28.                 return render(request, 'login.html', {'form': form, 'msg': "password or username is not ture!"})

  29.         else: ###如果提交的數(shù)據(jù)不合法,繼續(xù)登錄

  30.             return render(request, 'login.html', {'form': form})

  31. ###登出

  32. @login_required

  33. def log_out(request):

  34.     logout(request)

  35.     return redirect('login')

  36. # Create your views here.

  37. ###注冊(cè)

  38. def register(request):

  39.     error1 = "this name is already exist"

  40.     valid = "this name is valid"

  41.     if request.method == 'GET':

  42.         form = RegisterForm()

  43.         return render(request, 'register.html', {'form': form})

  44.     if request.method == 'POST': ###提交

  45.         form = RegisterForm(request.POST)

  46.         if form.is_valid():

  47.                 username = form.cleaned_data['username']

  48.                 email = form.cleaned_data['email']

  49.                 password1 = form.cleaned_data['password1']

  50.                 password2 = form.cleaned_data['password2']

  51.                 if password1 != password2:

  52.                     return render(request, 'register.html', {'form': form, 'msg': "two password is not equal"})

  53.                 else:

  54.                     NewUser.objects.create_user(username=username,password=password1,email=email)

  55.                     #user = NewUser(username=username, email=email, password=password1)

  56.                     #user.save()

  57.                     # return render(request, 'login.html', {'success': "you have successfully registered!"})

  58.                     return redirect('login')

  59.         else:

  60.                 return render(request, 'register.html', {'form': form})

4 首頁獲取當(dāng)前登錄用戶


  1. 方式一:
    前端request.user.is_authenticated()判斷當(dāng)前用戶是否登錄 


  2.         

  3.           {% if request.user.is_authenticated %}

  4.             hihi: {{ request.user }}

  5.           登出

  6.           {% endif %}


  7.       



方式二 將request.user.is_authenticated判斷寫入views視圖再傳入html中

  1. @login_required

  2. def index(request):

  3.     if request.user.is_authenticated(): # 判斷用戶是否已登錄

  4.         user = request.user;

  5.         return render_to_response('index.html', locals())




  6.  


  7.         

  8.              歡迎登錄!{{ user }}

  9.           登出

  10.           


  11.       

“Python Django怎么實(shí)現(xiàn)登錄注冊(cè)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!


網(wǎng)站題目:PythonDjango怎么實(shí)現(xiàn)登錄注冊(cè)
文章鏈接:http://weahome.cn/article/jcpdhs.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部