91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python Django怎么實現登錄注冊

發布時間:2021-12-30 17:42:45 來源:億速云 閱讀:143 作者:iii 欄目:編程語言

本篇內容介紹了“Python Django怎么實現登錄注冊”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

1.modles層 與admin

點擊(此處)折疊或打開

  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. ###普通用戶的數據模型

  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設置newuser管理系統賬號進行驗證

  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': # 當提交表單時

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

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

  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: ###如果用戶驗證存在,

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

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

  27.             else:

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


  29.         else: ###如果提交的數據不合法,繼續登錄

  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. ###注冊

  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 首頁獲取當前登錄用戶


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

  2. <form  class="form-signin" role="form" method='post'>

  3.         <h3 class="form-signin-heading">Welcome!>

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

  5.             hihi: {{ request.user }} </br>

  6.           <a href="{%   url 'log_out' %}">登出</a>

  7.           {% endif %}


  8.       </form>



方式二 將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.  <form  class="form-signin" role="form" method='post'>

  7.         <h3 class="form-signin-heading">Welcome!>

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

  9.           <a href="{%   url 'log_out' %}">登出</a>

  10.           


  11.       </form>

“Python Django怎么實現登錄注冊”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

巫溪县| 天水市| 清原| 阳信县| 庆安县| 宝丰县| 锦屏县| 丘北县| 广灵县| 景东| 中西区| 丹凤县| 长沙市| 义马市| 大庆市| 周至县| 七台河市| 马边| 鱼台县| 吴桥县| 宜阳县| 宝鸡市| 兴城市| 汤阴县| 洞口县| 台江县| 清涧县| 柯坪县| 沙田区| 西城区| 化隆| 瑞金市| 无锡市| 平原县| 洛宁县| 阳信县| 湖北省| 东阿县| 固始县| 阿拉善右旗| 泗阳县|