您好,登錄后才能下訂單哦!
本篇內容介紹了“Python Django怎么實現登錄注冊”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
點擊(此處)折疊或打開
# -*- 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
###普通用戶的數據模型
@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': # 當提交表單時
form = LoginForm(request.POST) # form 包含提交的數據
if form.is_valid(): # 如果提交的數據合法
username = form.cleaned_data['uid']
password = form.cleaned_data['pwd']
user = authenticate(username=username, password=password)
if user is not None: ###如果用戶驗證存在,
login(request, user) #開始登錄
return redirect('index') #跳轉首頁
else:
return render(request, 'login.html', {'form': form, 'msg': "password or username is not ture!"})
else: ###如果提交的數據不合法,繼續登錄
return render(request, 'login.html', {'form': form})
###登出
@login_required
def log_out(request):
logout(request)
return redirect('login')
# Create your views here.
###注冊
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()判斷當前用戶是否登錄
<form class="form-signin" role="form" method='post'>
<h3 class="form-signin-heading">Welcome!>
{% if request.user.is_authenticated %}
hihi: {{ request.user }} </br>
<a href="{% url 'log_out' %}">登出</a>
{% endif %}
</form>
方式二 將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())
<form class="form-signin" role="form" method='post'>
<h3 class="form-signin-heading">Welcome!>
歡迎登錄!{{ user }}
<a href="{% url 'log_out' %}">登出</a>
</form>
“Python Django怎么實現登錄注冊”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。