十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章将为大家详细讲解有关使用Django认证系统怎么实现web页面,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
目前成都创新互联已为成百上千家的企业提供了网站建设、域名、网站空间、成都网站托管、企业网站设计、平昌网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。一:数据模块
扩展了Django中的user表,增加了自定义的字段
from django.db import models from django.contrib.auth.models import AbstractUser # Create your models here. class UserInfo(AbstractUser): phone = models.CharField(max_length=11) gender = models.CharField(max_length=2)
二:路由系统
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^register/',views.register), url(r'^login/',views.login_view), url(r'^home/',views.home), url(r'^logout/',views.logout_view), url(r'^modify_pwd/',views.modify_pwd), url(r'^$',views.home), ]
三:视图系统
from django.shortcuts import render, redirect from django.contrib.auth.decorators import login_required from django.http import JsonResponse from django.contrib.auth import authenticate, login,logout from app01 import forms from app01.models import UserInfo # Create your views here. def register(request): form_obj = forms.Reg_form() if request.method == 'POST': form_obj = forms.Reg_form(request.POST) if form_obj.is_valid(): info_dic = form_obj.cleaned_data sex_dic = {'1':'男','2':'女','3':'保密'} info_dic['gender']=sex_dic[info_dic['gender']] UserInfo.objects.create_user( username=info_dic['username'], password = info_dic['pwd'], gender=info_dic['gender'], phone =info_dic['phone'] ) return redirect('/login/') return render(request, "register.html",{'form_obj':form_obj}) def login_view(request): if request.method == 'POST': username = request.POST.get('username') pwd = request.POST.get('pwd') user = authenticate(username=username, password=pwd) if user: login(request, user) data = {'code':1} else: data = {'code': 0,'msg':'用户名或密码错误'} return JsonResponse(data) return render(request, 'login.html') @login_required def logout_view(request): logout(request) return redirect('/login/') @login_required def home(request): user_id = request.session['_auth_user_id'] use_obj = request.user return render(request,'home.html',{'user':use_obj}) @login_required def modify_pwd(request): if request.method == 'POST': old_pwd = request.POST.get('old_pwd') pwd = request.POST.get('pwd') re_pwd = request.POST.get('re_pwd') user_obj = request.user if user_obj.check_password(old_pwd): if re_pwd == pwd: user_obj.set_password(pwd) user_obj.save() data = {'code': 1} else: data = {'code': 0, 'msg': '两次输入密码不一致'} else: data = {'code': 0, 'msg': '原始密码输入错误'} return JsonResponse(data) return render(request,'modify_pwd.html')
四:form表单
#!/usr/bin/env python # -*- coding: UTF-8 -*- # Author:YiJun from django import forms from app01 import models from django.forms import widgets from django.core.exceptions import ValidationError # 导入异常 import re # Create your views here. class Reg_form(forms.Form): # 用户名表单 username = forms.CharField( min_length=4, label="设置用户名", error_messages={ "required": "不能为空", "invalid": "格式错误", "min_length": "用户名最少4个字符" }, widget=widgets.TextInput( attrs={ 'class': "form-control", 'placeholder': '用户名' }) ) # 用户密码设置表单 pwd = forms.CharField( min_length=6, label="设置密码", widget=forms.widgets.PasswordInput( attrs={ 'class': 'form-control', 'placeholder': '密码'}, render_value=True, ), error_messages={ "required": "不能为空", "invalid": "格式错误", "min_length": "密码至少6位" } ) # 用户密码确认表单 r_pwd = forms.CharField( min_length=6, label="确认密码", widget=forms.widgets.PasswordInput( attrs={ 'class': 'form-control', 'placeholder': '确认密码'}, render_value=True, ), error_messages={ "required": "不能为空", "invalid": "格式错误", "min_length": "密码至少6位" } ) # 用户性别选择表单 gender = forms.ChoiceField( choices=((1, "男"), (2, "女"), (3, "保密")), label="性别", initial=3, widget=forms.widgets.RadioSelect ) # 用户手机号码表单 phone = forms.CharField( label="手机号码", max_length=11, min_length=11, error_messages={ "required": "不能为空", "invalid": "格式错误", "min_length": "手机号码至少11位", "max_length": "手机号码最多11位", }, widget=widgets.TextInput(attrs={'class': "form-control",'placeholder': '手机号码'}) ) def clean_phone(self): value = self.cleaned_data['phone'] expression = re.compile('^1[3589][0-9]{9}') if not expression.search(value).group(): raise ValidationError('请输入正确的手机号码') else: return value def clean_username(self): value = self.cleaned_data['username'] if models.UserInfo.objects.filter(username=value): raise ValidationError('用户名已经注册') else: return value def clean(self): pwd = self.cleaned_data.get("pwd") r_pwd = self.cleaned_data.get("r_pwd") if pwd != r_pwd: self.add_error("r_pwd", "两次输入的密码不一致!") # 两次输入的密码不一致 raise ValidationError("两次输入的密码不一致!") else: self.cleaned_data.pop('r_pwd') return self.cleaned_data
五:模板系统
注册页面
Document home.html
关于使用Django认证系统怎么实现web页面就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。