周末手撸管理系统(一)

周末手撸管理系统(一)

已完成

大体模板

  • 用户登入注册

明天加商品订单商品进去

完成效果图

注册

登入

1.首先进行设置

settings.py

"""
Django settings for drf_test project.

Generated by 'django-admin startproject' using Django 1.11.22.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'ppa5l3jvxr4k^ow*4o+0_^[email protected]&sa3x+!hb_$artwraa%[email protected]'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'drf_api',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'drf_test.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'drf_test.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS=(os.path.join(BASE_DIR,'static'),)
AUTH_USER_MODEL = "drf_api.UserInfo"
MEDIA_URL  = "/img/"
MEDIA_ROOT = os.path.join(BASE_DIR, "img")

2.创建模型以及form组件

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class UserInfo(AbstractUser):
    avatar = models.FileField(upload_to='avatar/', default='avatar/default.png')
    class Meta:
        verbose_name='用户表'
        verbose_name_plural = verbose_name

userinfo_form.py

from django.shortcuts import render,HttpResponse,redirect
from django.http import JsonResponse
from PIL import Image,ImageDraw,ImageFont
import random
from io import BytesIO
from django.contrib import auth
from drf_api.userinfo_form import Register
from drf_api import models
from django.db.models import Count
from django.db.models.functions import TruncMonth
from django.db.models import F

def register(request):
    if request.method=='GET':
        form=Register()
        return render(request,'register.html',{'form':form})
    elif request.is_ajax():
        response={'code':100,'msg':None}
        form = Register(request.POST)
        if form.is_valid():
            #校验通过的数据
            clean_data=form.cleaned_data
            #把re_pwd剔除
            clean_data.pop('re_pwd')
            #取出头像
            avatar=request.FILES.get('avatar')
            if avatar:
                #因为用的是FileField,只需要把文件对象赋值给avatar字段,自动做保存
                clean_data['avatar']=avatar
            user=models.UserInfo.objects.create_user(**clean_data)
            if user:
                response['msg'] = '创建成功'
            else:
                response['code'] = 103
                # 把校验不通过的数据返回
                response['msg'] = '创建失败'
        else:
            response['code']=101
            #把校验不通过的数据返回
            response['msg']=form.errors
            print(type(form.errors))
        return JsonResponse(response,safe=False)

def login(request):
    if request.method=='GET':
        return render(request,'login.html')
    else:
        print(request.POST)
        user_name=request.POST.get('name')
        pwd=request.POST.get('pwd')
        code=request.POST.get('code')

        user=auth.authenticate(username=user_name,password=pwd)
        print(user)
        if request.session.get('code').upper() !=code.upper(): #忽略大小写
            return HttpResponse('验证码错误')
        elif not user:
            return HttpResponse('账号密码错误')
        else:
            return HttpResponse('登入成功')

def get_code(request):
    if request.method == 'GET':
        img = Image.new('RGB', (350, 40), (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
        # 写文字
        # 生成一个字体对象
        font = ImageFont.truetype('/static/Gabriola.ttf', 34)
        # 调用方法,返回一个画板对象
        draw = ImageDraw.Draw(img)

        new_text =''
        # 生成随机8位数字
        for x_index in range(1, 8):
            num = chr(random.randint(48, 57))
            word = chr(random.randint(65, 90))
            word_1 = chr(random.randint(97, 122))
            text =random.choice((num, word, word_1))
            draw.text((x_index * 32, 0),text, font=font)
            new_text +=text

        # 加点线
        width = 320
        height = 35
        for i in range(5):
            x1 = random.randint(0, width)
            x2 = random.randint(0, width)
            y1 = random.randint(0, height)
            y2 = random.randint(0, height)
            # 在图片上画线
            draw.line((x1, y1, x2, y2), fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))

        for i in range(33):
            # 画点
            draw.point([random.randint(0, width), random.randint(0, height)], fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
            x = random.randint(0, width)
            y = random.randint(0, height)
            # 画弧形
            draw.arc((x, y, x + 4, y + 4), 0, 90, fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
        print(new_text)
        #存在session中
        request.session['code']=new_text
        #存内存
        f = BytesIO()
        img.save(f, 'png')
        return HttpResponse(f.getvalue())

3.页面

母版

template.html

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
    <script src="/static/jquery-3.4.1.min.js"></script>
    <script src="/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
    <title>{% block title_text %}{% endblock %}</title>
</head>
<style>
    .header {
        position: fixed;
        top: 0;
        left: 0;
        height: 100px;
        width: 100%;
        background: red;
    }
    .header_left{

        position: absolute;
        line-height: 100px;
        font-size: 50px;
        width:20% ;
        text-align: center;
        background: black;
        color: white;
    }
    .header_right{
        float: right;
                line-height: 100px;
    }
    .body {
        position: fixed;
        top: 100px;
        left: 0;
        bottom: 20px;
        width: 100%;
    }

    .body_left {
        position: fixed;
        top: 100px;
        left: 0;
        bottom: 20px;
        width: 20%;
        background: white;
    }
    .body_right {
        overflow: auto;
        position: fixed;
        top: 100px;
        left: 20%;
        bottom: 20px;
        width: 80%;
        background: #2aabd2;
    }

    .footer {
        position: fixed;
        left: 0;
        bottom: 0;
        height: 20px;
        width: 100%;
        background: black;
    }
</style>
{% block new_css %}
{% endblock %}
<body>
<div class="header">
    <div class="header_left">管理系统</div>
    <div class="header_right">
        <a href="http://127.0.0.1:8000/login/">登入</a>
        <a href="http://127.0.0.1:8000/register/">注册</a>
    </div>
</div>
<div class="body">
    <div class="body_left">
        <ul class="nav nav-pills nav-stacked nav-pills-stacked-example">
      <li role="presentation" class="active"><a>库存管理</a></li>
         <li role="presentation"><a href="#">查看商品</a></li>
      <li role="presentation"><a href="#">添加商品</a></li>
      <li role="presentation"><a href="#">删除商品</a></li>
    </ul>
                <ul class="nav nav-pills nav-stacked nav-pills-stacked-example">
      <li role="presentation" class="active"><a>订单管理</a></li>
       <li role="presentation"><a href="#">查看订单</a></li>
      <li role="presentation"><a href="#">添加订单</a></li>
      <li role="presentation"><a href="#">删除订单</a></li>
      <li role="presentation"><a href="#">订单号快速查询</a></li>
    </ul>
    </div>
    <div class="body_right">
        {% block data %}
        {% endblock %}
    </div>
</div>
<div class="footer"></div>
</body>
</html>
{% block new_js %}
{% endblock %}

登入

login.html

{% extends 'template.html' %}
{% block title_text %}
    登入
{% endblock %}
{% block new_css %}
{% endblock %}
{% block data %}
    <div class="container-fluid center-block">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                {% csrf_token %}
                <h1>登陆</h1>
                <form action="">
                    <div class="form-group">
                        <label for="id_name">用户名</label>
                        <input type="text" name="name" id="id_name" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="pwd">密码</label>
                        <input type="password" name="pwd" id="pwd" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="id_code">验证码</label>
                        <div class="row">
                            <div class="col-md-6">
                                <input type="text" name="code" id="id_code" class="form-control">
                            </div>
                            <div class="col-md-6" id="img">
                                <img src="/get_code/" height="40" width="240" class="img-code">
                            </div>
                        </div>
                    </div>
                    <input type="button" value="提交" class="btn-success" id="up_data">
                    <span style="color: red" id="msg"></span>
                </form>
            </div>
        </div>
    </div>
{% endblock %}
{% block new_js %}
    <script>
        $('.img-code').click(function () {
            var img_code_src = $(this).attr('src');
            img_code_src += '1';
            console.log(img_code_src);
            $(this).attr('src', img_code_src)
        })
    </script>

    <script>
        $('#up_data').click(function () {
            $.ajax({
                type: 'post',
                url: '/login/',
                data: {
                    'name': $('#id_name').val(),
                    'pwd': $('#pwd').val(),
                    'code': $('#id_code').val(),
                    'csrfmiddlewaretoken': '{{csrf_token}}'
                },
                success: function (msg) {
                    console.log(msg);
                    $('#msg').text(msg);
                    if (msg == '登入成功') {
                        console.log('sb');
                        window.location.replace('http://www.baidu.com');
                    }
                }
            })
        })
    </script>
{% endblock %}

登入

register.html

{% extends 'template.html' %}
{% block title_text %}
    注册
{% endblock %}
{% block new_css %}
{% endblock %}
{% block data %}
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">

                <h1>注册</h1>
                <form id="my_form">
                    {% for foo in form %}
                        <div class="form-group">
                            <label for="{{ foo.auto_id }}">{{ foo.label }}</label>
                            {{ foo }} <span style="color: red" class="error pull-right"></span>
                        </div>

                    {% endfor %}
                    <div class="form-group">
                        <label for="id_file">头像
                            <img src="/img/avatar/default.png" width="80" height="80" style="margin-left: 20px"
                                 id="id_img">
                        </label>

                        <input type="file" name="file" id="id_file" style="display: none">
                    </div>
                    <input type="button" class="btn btn-success" value="提交" id="id_submit">
                </form>
            </div>
        </div>
    </div>
{% endblock %}
{% block new_js %}
    <script>
        $("#id_file").change(function () {
            var file = $("#id_file")[0].files[0]
            var filereader = new FileReader()
            filereader.readAsDataURL(file)
            filereader.onload = function () {
                $("#id_img").attr('src', filereader.result)
            }
        })
        $("#id_submit").click(function () {
            var formdata = new FormData()
            var my_form_data = $("#my_form").serializeArray()
            $.each(my_form_data, function (k, v) {
                {#console.log(k)#}
                {#console.log(v)#}
                formdata.append(v.name, v.value)
            })
            formdata.append('avatar', $("#id_file")[0].files[0])

            $.ajax({
                url: '/register/',
                type: 'post',
                processData: false,
                contentType: false,
                data: formdata,
                success: function (data) {
                    //console.log(data)
                    if (data.code == 100) {
                        location.href = '/login/'
                    } else if (data.code == 101) {
                        $.each(data.msg, function (k, v) {
                            console.log(k)
                            console.log(v)
                            $("#id_" + k).next().html(v[0])
                            if (k == '__all__') {
                                $("#id_re_pwd").next().html(v[0])
                            }
                        })
                    }
                    //定时器
                    setTimeout(function () {
                        $(".error").html("")
                    }, 3000)
                }
            })
        })
    </script>
{% endblock %}

4.路由

urls.py

from django.conf.urls import url
from django.contrib import admin
from drf_api import views
from django.views.static import serve
from .settings import MEDIA_ROOT
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^register/', views.register),
    url(r'^login/', views.login),
    url(r'^get_code/', views.get_code),
    url(r'^img/(?P<path>.*)', serve, {'document_root': MEDIA_ROOT}),
]

5.视图

from django.shortcuts import render,HttpResponse,redirect
from django.http import JsonResponse

from PIL import Image,ImageDraw,ImageFont
import random

from io import BytesIO

from django.contrib import auth

from drf_api.userinfo_form import Register
from drf_api import models

def register(request):
    if request.method=='GET':
        form=Register()
        return render(request,'register.html',{'form':form})
    elif request.is_ajax():
        response={'code':100,'msg':None}
        form = Register(request.POST)
        if form.is_valid():
            #校验通过的数据
            clean_data=form.cleaned_data
            #把re_pwd剔除
            clean_data.pop('re_pwd')
            #取出头像
            avatar=request.FILES.get('avatar')
            if avatar:
                clean_data['avatar']=avatar
            user=models.UserInfo.objects.create_user(**clean_data)
            if user:
                response['msg'] = '创建成功'
            else:
                response['code'] = 103
                response['msg'] = '创建失败'
        else:
            response['code']=101
            #把校验不通过的数据返回
            response['msg']=form.errors
            print(type(form.errors))
        return JsonResponse(response,safe=False)

def login(request):
    if request.method=='GET':
        return render(request,'login.html')
    else:
        print(request.POST)
        user_name=request.POST.get('name')
        pwd=request.POST.get('pwd')
        code=request.POST.get('code')

        user=auth.authenticate(username=user_name,password=pwd)
        print(user)
        if request.session.get('code').upper() !=code.upper(): #忽略大小写
            return HttpResponse('验证码错误')
        elif not user:
            return HttpResponse('账号密码错误')
        else:
            return HttpResponse('登入成功')

def get_code(request):
    if request.method == 'GET':
        img = Image.new('RGB', (350, 40), (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
        # 写文字
        # 生成一个字体对象
        font = ImageFont.truetype('/static/Gabriola.ttf', 34)
        # 调用方法,返回一个画板对象
        draw = ImageDraw.Draw(img)

        new_text =''
        # 生成随机8位数字
        for x_index in range(1, 8):
            num = chr(random.randint(48, 57))
            word = chr(random.randint(65, 90))
            word_1 = chr(random.randint(97, 122))
            text =random.choice((num, word, word_1))
            draw.text((x_index * 32, 0),text, font=font)
            new_text +=text

        # 加点线
        width = 320
        height = 35
        for i in range(5):
            x1 = random.randint(0, width)
            x2 = random.randint(0, width)
            y1 = random.randint(0, height)
            y2 = random.randint(0, height)
            # 在图片上画线
            draw.line((x1, y1, x2, y2), fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))

        for i in range(33):
            # 画点
            draw.point([random.randint(0, width), random.randint(0, height)], fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
            x = random.randint(0, width)
            y = random.randint(0, height)
            # 画弧形
            draw.arc((x, y, x + 4, y + 4), 0, 90, fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
        print(new_text)
        #存在session中
        request.session['code']=new_text
        #存内存
        f = BytesIO()
        img.save(f, 'png')
        return HttpResponse(f.getvalue())

大家周末娱乐

原文地址:https://www.cnblogs.com/pythonywy/p/11483157.html

时间: 2024-10-10 03:13:34

周末手撸管理系统(一)的相关文章

编译原理 - 1 手撸状态机词法分析器

感谢vczh轮子叔的坑了的教程,我的编译原理第一次入了个门,词法分析写完了,今后可以看看书继续往下学了. http://www.cppblog.com/vczh/archive/2014/03/02/206014.html 词法分析,就是对于一段代码,把他们分割成一个个的token,同时记录他们的行列号,丢掉不必要的信息,这个词法分析器很简单,简单的状态机就能胜任,用正则就没有自己造轮子的快感了,所以要自己手撸状态机拆token出来. 模仿vczh的语言,我的语言包括了以下要素 标识符:大小写字

手撸kmeans(c++)实现,用于图像的主色调提取(1)

想到那天头条面试时,让我手撸kmeans,奈何好久不用c++,好多都忘了==淡淡的忧伤 这次刚好赶上机会,可以再试试了,我写成项目了,有多个文件 首先:base.h #ifndef BASE_H #define BASE_H #include<iostream> #include<opencv2/opencv.hpp> #include<cassert> #include<stdlib.h> class Baseofgeo{ public: float co

Haskell手撸Softmax回归实现MNIST手写识别

Haskell手撸Softmax回归实现MNIST手写识别 前言 初学Haskell,看的书是Learn You a Haskell for Great Good, 才刚看到Making Our Own Types and Typeclasses这一章. 为了加深对Haskell的理解,便动手写了个Softmax回归.纯粹造轮子,只用了base. 显示图片虽然用了OpenGL,但是本文不会提到关于OpenGL的内容.虽说是造轮子, 但是这轮子造得还是使我受益匪浅.Softmax回归方面的内容参考

以鶸ice为例,手撸一个解释器(一)明确目标

代码地址 # HelloWorld.ice print("hello, world") 前言(废话) 其实从开始学习编译原理到现在已经有快半年的时间了,但是其间常常不能坚持看下去龙书(经常三天打鱼两天晒网,更何况每次打鱼不到半小时就累得不行又会放下书(笑)),截至到现在只勉强看完了前六章的部分,半年间其它事也没有做,其实想想上大学已经快两年了还是一事无成,知识也没有学到,不免觉得很羞愧. 暑假也要到了,这个学期马上也要结束了,临近大二结束之际,还是尝试着写一下以前想写的玩具吧,而本系列

使用Java Socket手撸一个http服务器

原文连接:使用Java Socket手撸一个http服务器 作为一个java后端,提供http服务可以说是基本技能之一了,但是你真的了解http协议么?你知道知道如何手撸一个http服务器么?tomcat的底层是怎么支持http服务的呢?大名鼎鼎的Servlet又是什么东西呢,该怎么使用呢? 在初学java时,socket编程是逃不掉的一章:虽然在实际业务项目中,使用这个的可能性基本为0,本篇博文将主要介绍如何使用socket来实现一个简单的http服务器功能,提供常见的get/post请求支持

php手撸轻量级开发(一)

聊聊本文内容 之前讲过php简单的内容,但是原生永远是不够看的,这次用框架做一些功能性的事情. 但是公司用自己的框架不能拿出来,用了用一些流行的框架比如tp,larveral之类的感觉太重,CI也不顺手,于是整体框架就用之前在幕客上看的这个,顺便自己手动改吧改吧,看哪不爽改哪里,一个精简的框架就出来了. 也在做java,但正是php这种能让人一眼看明白的精简的架构吸引了我,没有过多的设计和复杂的实现,整个项目就是明明白白聚焦于业务就好,怎么爽怎么来就好了. 前置的要求 具有一点php的功底.这个

【手撸一个ORM】使用说明

传送门 [手撸一个ORM]第一步.约定和实体描述 [手撸一个ORM]第二步.封装实体描述和实体属性描述 [手撸一个ORM]第三步.SQL语句构造器和SqlParameter封装 [手撸一个ORM]第四步.Expression(表达式目录树)扩展 [手撸一个ORM]第五步.查询条件表达式目录树解析和插入.更新查询目录树解析 [手撸一个ORM]第六步.对象表达式解析和Select表达式解析 [手撸一个ORM]第七步.SqlDataReader转实体 [手撸一个ORM]第八步.实体查询和按需查询 [手

Python Django前奏手撸web框架 基于wsgiref的web框架

基于socke手撸web框架 # 请求首行 b'GET / HTTP/1.1\r\n #GET /index HTTP/1.1\r\n (index)为浏览器请求地址) # 请求头 Host: 127.0.0.1:8080\r\n Connection: keep-alive\r\n Cache-Control: max-age=0\r\n Upgrade-Insecure-Requests: 1\r\n # User-Agent用来标识是不是浏览器 User-Agent: Mozilla/5.

纯手撸web框架

纯手撸web框架 一.Web应用的组成 接下来我们学习的目的是为了开发一个Web应用程序,而Web应用程序是基于B/S架构的,其中B指的是浏览器,负责向S端发送请求信息,而S端会根据接收到的请求信息返回相应的数据给浏览器,需要强调的一点是:S端由server和application两大部分构成,如图所示: 上图:Web应用组成 二.手鲁web应用 2.1 基本版 import socket """ @author RansySun @create 2019-10-18-11: