金婚片头曲,花野真衣红衣校花,全国同名查询
已完成
大体模板
明天加商品订单商品进去
完成效果图
注册
登入
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_^7@&sa3x+!hb_$artwraa%60iq@g7' # 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")
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())
母版
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 %}
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}), ]
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())
大家周末娱乐
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python 实现将numpy中的nan和inf,nan替换成对应的均值
python爬虫把url链接编码成gbk2312格式过程解析
网友评论