🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
配置models.py文件 ~~~ # _*_ coding:utf-8 * _*_ from __future__ import unicode_literals from datetime import datetime from django.db import models from django.contrib.auth.models import User#引入自带User # # # Create your models here. #新增用户注册字段 class UserProfile(models.Model): #OneToOneField通过user字段与User类关联,一对一 user = models.OneToOneField(User, unique=True, verbose_name=u"帐号") birth = models.DateField(blank=True, null=True, verbose_name=u"生日") phone = models.CharField(max_length=11, null=True, verbose_name=u"手机号") class Meta: verbose_name = u"用户信息" verbose_name_plural = verbose_name def __str__(self): return 'user{}'.format(self.user.username) #新增用户信息 class UserInfo(models.Model): user = models.OneToOneField(User, unique=True) shool = models.CharField(max_length=100, blank=True) profession = models.CharField(max_length=100, blank=True) address = models.CharField(max_length=100, blank=True) aboutme = models.TextField(blank=True) def __str__(self): return "user:{}".format(self.user.username) ~~~ 配置views.py文件 ~~~ # _*_ coding:utf-8 * _*_ from django.shortcuts import render, redirect#redirect跳转 #引入系统模块 from django.http import HttpResponse #引入login内置用户管理 from django.contrib.auth import authenticate, login #引入forms类 from .forms import LoginFrom, RegistrationFrom, UserProfileForm #UserProfileForm新增用户字段 #新增用户信息,required引入装饰器,判断用户登录 from django.contrib.auth.decorators import login_required from .models import UserProfile, UserInfo from django.contrib.auth.models import User # Create your views here. #登录函数 def user_login(request): #method是HttpRequest对象的一个常用属性,可返回POST、GET if request.method == "POST": login_form = LoginFrom(request.POST) #is_valid验证传入数据是否合法性 if login_form.is_valid(): #cleaned_data引用一个字典类型数据 cd = login_form.cleaned_data #authenticate检验数据库帐号密码是否正确 user = authenticate(username=cd['username'], password=cd['password']) #login引入的login()函数,已语句user实例对象作为参数,实现用户登录,登录后调用session if user: #判断正确返回HttpResponse login(request, user) #redirect跳转 return redirect("blog:blog_title") #return HttpResponse("Wellcome YOU") else: return HttpResponse("Sorry") if request.method == "GET": login_form = LoginFrom() return render(request, "account/login.html", {"form":login_form}) #注册函数,原始版 # def register(request): # if request.method == "POST": # #用这些数据实例化一个用户注册表单 # user_form = RegistrationFrom(request.POST) # # 验证数据的合法性 # if user_form.is_valid(): # #.save(commit=False)数据不保存数据库,仅生成数据对象, # new_user = user_form.save(commit=False) # #.cleaned_data['password']校验密码然后保存数据库 # new_user.set_password(user_form.cleaned_data['password']) # #保存数据库 # new_user.save() # #redirect跳转 # return redirect("blog:blog_title") # # return HttpResponse("successfully") # else: # return HttpResponse("sorry,your can not register") # else: # # 请求不是 POST,构造一个空表单 # user_form = RegistrationFrom() # # 渲染模板 # # 如果不是 POST 请求,则渲染的是一个空的表单 # # 如果用户通过表单提交数据,但是数据验证不合法,则渲染的是一个带有错误信息的表单 # return render(request, "account/register.html", {"form": user_form}) #注册函数,新增字段 def register(request): if request.method == "POST": user_form = RegistrationFrom(request.POST) #新增字段 userprofile_form = UserProfileForm(request.POST) #新增*userprofile_form.is_valid()验证数据 if user_form.is_valid()*userprofile_form.is_valid(): new_user = user_form.save(commit=False) new_user.set_password(user_form.cleaned_data['password']) new_user.save() #新增字段 new_profile = userprofile_form.save(commit=False) new_profile.user = new_user new_profile.save() UserInfo.objects.create(user=new_user)##新增个人信息 return redirect("blog:blog_title") # return HttpResponse("successfully") else: return HttpResponse("sorry,your can not register") else: user_form = RegistrationFrom() return render(request, "account/register.html", {"form": user_form, "profile": UserProfileForm}) #新增个人信息 @login_required(login_url='account:user_login') def myself(request): user = User.objects.get(username=request.user.username) userprofile = UserProfile.objects.get(user=user) userinfo = UserInfo.objects.get(user=user) return render(request, "account/myself.html", {"user": user, "userinfo": userinfo, "userprofile": userprofile}) ~~~ 配置urls.py文件 ~~~ # _*_ coding:utf-8 _*_ __author__ = 'canmeng' __date__ = '2018/1/7 19:16' from django.conf.urls import url #引入app项目 view from .import views from django.conf import settings #引入系统自动登录views from django.contrib.auth import views as auth_views urlpatterns = [ url(r'^login/$', views.user_login, name="user_login"),#自建系统登录 #url(r'^login/$', auth_views.login, name="user_login"),#引入系统自带views登录 # url(r'^logout/$', auth_views.logout, name="user_logout"),#引入系统自带views退出 #用户退出,定义连接"template_name": "account/logout.html" url(r'^logout/$', auth_views.logout, {"template_name": "account/logout.html"}, name="user_logout"),#退出登录 url(r'^register/$', views.register, name="register"),#注册 url(r'^myself/$', views.myself, name="myself"),#注册 ] ~~~ myself.html样板 ~~~ {% extends "base.html" %} {% block title %}用户信息{% endblock %} {% block content %} <div> <div>用户名</div> <div>{{ user.username }}</div> </div> <div> <div>邮箱</div> <div>{{ user.email }}</div> </div> <div> <div>生日</div> <div>{{ userprofile.birth }}</div> </div> <div> <div>电话</div> <div>{{ userprofile.phone }}</div> </div> <div> <div>毕业学校</div> <div>{{ userinfo.shool }}</div> </div> <div> <div>职业</div> <div>{{ userinfo.profession }}</div> </div> <div> <div>地址</div> <div>{{ userinfo.address }}</div> </div> <div> <div>个人介绍</div> <div>{{ userinfo.aboutme }}</div> </div> {% endblock %} ~~~