本篇內(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
點(diǎn)擊(此處)折疊或打開
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.encoding import python_2_unicode_compatible
###普通用戶的數(shù)據(jù)模型
@python_2_unicode_compatible
class NewUser(AbstractUser):
profile= models.CharField('profile', default='', max_length=256)
def __str__(self):
return self.username
# 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)
AUTH_USER_MODEL = "pric.NewUser"
# -*- coding: utf-8 -*-
from django.shortcuts import render
from .forms import *
from .models import *
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponse
from django.shortcuts import render, redirect,render_to_response
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.decorators import login_required
@login_required
def index(request):
return render_to_response('index.html', locals())
###登錄
def log_in(request):
if request.method == 'GET':
form = LoginForm()
return render(request, 'login.html', {'form': form})
if request.method == 'POST': # 當(dāng)提交表單時(shí)
form = LoginForm(request.POST) # form 包含提交的數(shù)據(jù)
if form.is_valid(): # 如果提交的數(shù)據(jù)合法
username = form.cleaned_data['uid']
password = form.cleaned_data['pwd']
user = authenticate(username=username, password=password)
if user is not None: ###如果用戶驗(yàn)證存在,
login(request, user) #開始登錄
return redirect('index') #跳轉(zhuǎn)首頁
else:
return render(request, 'login.html', {'form': form, 'msg': "password or username is not ture!"})
else: ###如果提交的數(shù)據(jù)不合法,繼續(xù)登錄
return render(request, 'login.html', {'form': form})
###登出
@login_required
def log_out(request):
logout(request)
return redirect('login')
# Create your views here.
###注冊(cè)
def register(request):
error1 = "this name is already exist"
valid = "this name is valid"
if request.method == 'GET':
form = RegisterForm()
return render(request, 'register.html', {'form': form})
if request.method == 'POST': ###提交
form = RegisterForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
email = form.cleaned_data['email']
password1 = form.cleaned_data['password1']
password2 = form.cleaned_data['password2']
if password1 != password2:
return render(request, 'register.html', {'form': form, 'msg': "two password is not equal"})
else:
NewUser.objects.create_user(username=username,password=password1,email=email)
#user = NewUser(username=username, email=email, password=password1)
#user.save()
# return render(request, 'login.html', {'success': "you have successfully registered!"})
return redirect('login')
else:
return render(request, 'register.html', {'form': form})
方式一:
前端request.user.is_authenticated()判斷當(dāng)前用戶是否登錄
Welcome!>
{% if request.user.is_authenticated %}
hihi: {{ request.user }}
{% endif %}
方式二 將request.user.is_authenticated判斷寫入views視圖再傳入html中
@login_required
def index(request):
if request.user.is_authenticated(): # 判斷用戶是否已登錄
user = request.user;
return render_to_response('index.html', locals())
Welcome!>
歡迎登錄!{{ user }}
“Python Django怎么實(shí)現(xiàn)登錄注冊(cè)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!