栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

django 简单的注册登录后,进入个人页面,未登录进入个人主页, 登录后点击进入个人主页,注册登录简单的加密

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

django 简单的注册登录后,进入个人页面,未登录进入个人主页, 登录后点击进入个人主页,注册登录简单的加密

 文件目录结构

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())
    



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/929041.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号