文件目录结构
models.py
from unicodedata import name
from django.db import models
# Create your models here.
class User(models.Model):
id = models.AutoField(primary_key=True)
user_name = models.CharField(max_length=16,unique=True)
user_username = models.CharField(max_length=16,unique=True)
user_password = models.CharField(max_length=200,null=False)
python manage.py makemigrations
迁移文件
python manage.py migrate
执行迁移
url.py文件
from django.urls import path
from myapp import views
urlpatterns = [
path('',views.index,name="index"), # 首页
path('user/zhuce',views.logins,name="signIn"), # 登录模板
path('login/',views.login,name="sign"), # 登录逻辑判断
path('good/',views.good,name="good"), # 个人页面
path('zhuce/',views.zhuce,name="zhuce"), # 注册模板
path('zc/',views.zc,name="zc"), # 注册逻辑判断
path("tuichu/",views.tuichu,name="tuichu") # 退出登录
]
新建文件项目文件中 该应用下创建encrypt.py文件 md5加密
import hashlib
def md5(password):
h = hashlib.md5()
# 通过hashlib调用sha256方法进行加密
password_str =str(password)
# 根据需要转换要进行加密的变量值
h.update(bytes(password_str,encoding='utf-8'))
# update((bytes(变量,编码))方法现在只接受bytes类型的数据,不接收str类型。
return h.hexdigest()
新建模板文件 index.html 首页
Document
注册
登录
退出
登录后才能进入的页面不然进入到登录
新建模板文件 login.html 登录页
Document
新建模板文件 zhuce.html 登录页
Document
新建模板文件 goods.html 个人页
Document
你已登录,查看我的商品
views.py 文件
from myapp.models import User
from django.shortcuts import render,redirect,HttpResponse
from myapp.encrypt import md5
# 首页
def index(request):
return render(request,"myuser/index.html")
# 登录模板
def logins(request):
return render(request,"myuser/login.html")
# 用户的个人页
def good(request):
# 根据判断——游览器是否存在这个cookis
if request.COOKIES.get("username",None):
# 判断cookie的值 根据判断跳转对应页面
username = request.COOKIES.get("username")
if User.objects.filter(user_username=username):
return render(request,"myuser/goods.html")
return render(request,"myuser/login.html")
return render(request,"myuser/login.html")
# 退出
def tuichu(request):
response = HttpResponse("您已经成功退出")
response.delete_cookie("username")
return HttpResponse('')
# 登录页登录默认跳转到个人资产页
def login(request):
username = request.POST.get("username")
password = request.POST.get("password")
if User.objects.filter(user_username=username,user_password=password).exists():
# 登录成功重定向的个人页后向有游览器添加了cookie
response= redirect("good")
response.set_cookie("username",username)
return response
else:
centext = {"exit":"登录失败"}
return render(request,"myuser/login.html",centext)
# 注册模板
def zhuce(request):
return render(request,"myuser/zhuce.html")
# 注册页判断注册逻辑
def zc(request):
user=User()
name = request.POST.get("name")
username = request.POST.get("username")
password = request.POST.get("password")
passwords = request.POST.get("password_s")
if password !="" or passwords !="" :
if password==passwords:
user.user_name = name
user.user_username = username
user.user_password = md5(password)
user.save()
return render(request,"myuser/login.html")
else:
fanhui = "密码不一致"
print(fanhui)
return render(request,"myuser/zhuce.html",locals())
fanhui = "账号或用户名不能为空"
print(fanhui)
return render(request,"myuser/zhuce.html",locals())



