國慶有點懶散更新的內容很少,你們國慶快樂
html
上次寫到點我查看python
1iO0
這些讓人難以區分的內容setting.py
mysql
""" 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.mysql', # 數據庫引擎 'NAME': 'mib', # 你要存儲數據的庫名,事先要建立之 'USER': 'root', # 數據庫用戶名 'PASSWORD': '16745', # 密碼 'HOST': 'localhost', # IP 'PORT': '3306', # 數據庫使用的端口 } } # 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 = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False # 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")
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 rest_framework.throttling import SimpleRateThrottle def test(request): return render(request, 'aa.html') 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: auth.login(request,user) 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 ='' x = 100 # 生成隨機8位數字 text_chiose = 'zxcvbnmasdfghjklqwertyup23456789ZXCVBNMASDFGHJKLQWERTYUP' text_list=random.sample(text_chiose,8) for text in text_list: x+=20 y = random.randint(1,5) draw.text((x, y), text, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), font=font) new_text +=text # 加點線 width = 320 height = 35 for i in range(2): 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(10): # 畫點 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())
from django.db import models from django.contrib.auth.models import AbstractUser from rest_framework import mixins # 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 class Commodity(models.Model): id = models.SmallIntegerField(primary_key=True,db_column='序號') name = models.CharField(max_length=60,db_column='商品名',null=True) inventory = models.IntegerField(db_column='庫存',null=True) cost = models.DecimalField (max_digits=60,decimal_places=2,db_column='單件成本',null=True) selling_price= models.DecimalField (max_digits=60,decimal_places=2,db_column='銷售價格',null=True) quantity_in = models.IntegerField(db_column='進貨總數量',null=True) sales_volume = models.IntegerField(db_column='銷售量總數量',null=True) return_volume = models.IntegerField(db_column='退貨總數量',null=True) is_delete = models.BooleanField(default=False, db_column='是否刪除') def __str__(self): return self.name class Meta: db_table = "庫存表" class DaySales(models.Model): id = models.SmallIntegerField(primary_key=True, db_column='日期序號') date = models.DateTimeField(auto_now_add=True,db_column='日期') number = models.DecimalField(max_digits=60, decimal_places=2, db_column='營業總額', null=True) is_delete = models.BooleanField(default=False, db_column='是否刪除') date_changed = models.DateTimeField(auto_now=True,db_column='最後修改日期') def __str__(self): return self.date class Meta: db_table = "日銷售表" class DayStock(models.Model): id = models.SmallIntegerField(primary_key=True, db_column='日期序號') date = models.DateTimeField(auto_now_add=True,db_column='日期') number = models.DecimalField(max_digits=60, decimal_places=2, db_column='進貨總額', null=True) is_delete = models.BooleanField(default=False, db_column='是否刪除') date_changed = models.DateTimeField(auto_now=True,db_column='最後修改日期') def __str__(self): return self.date class Meta: db_table = "日進貨表" class DayReturn(models.Model): id = models.SmallIntegerField(primary_key=True, db_column='日期序號') date = models.DateTimeField(auto_now_add=True,db_column='日期') number = models.DecimalField(max_digits=60, decimal_places=2, db_column='退貨總額', null=True) is_delete = models.BooleanField(default=False, db_column='是否刪除') date_changed = models.DateTimeField(auto_now=True,db_column='最後修改日期') def __str__(self): return self.date class Meta: db_table = "日退貨表" class CommoditySales(models.Model): id = models.SmallIntegerField(primary_key=True, db_column='日期序號') name = models.CharField(max_length=60,db_column='商品名',null=True) data = models.ForeignKey('DaySales','id',db_column='銷售日期',null=True) selling_price= models.DecimalField (max_digits=60,decimal_places=2,db_column='銷售價格',null=True) sales_volume = models.IntegerField(db_column='銷售量數量',null=True) batch = models.IntegerField(db_column='批號',null=True) is_delete = models.BooleanField(default=False, db_column='是否刪除') date_changed = models.DateTimeField(auto_now=True,db_column='最後修改日期') def __str__(self): return self.name class Meta: db_table = "商品銷售表" class CommodityStock(models.Model): id = models.SmallIntegerField(primary_key=True, db_column='日期序號') name = models.CharField(max_length=60,db_column='商品名',null=True) data = models.ForeignKey('DayStock','id',db_column='進貨日期',null=True) cost = models.DecimalField (max_digits=60,decimal_places=2,db_column='單件成本',null=True) Stock_volume = models.IntegerField( db_column='進貨數量', null=True) batch = models.IntegerField(db_column='批號',null=True) is_delete = models.BooleanField(default=False, db_column='是否刪除') date_changed = models.DateTimeField(auto_now=True,db_column='最後修改日期') def __str__(self): return self.name class Meta: db_table = "商品進貨表" class CommodityReturn(models.Model): id = models.SmallIntegerField(primary_key=True, db_column='日期序號') name = models.CharField(max_length=60,db_column='商品名',null=True) data = models.ForeignKey('DayReturn','id',db_column='退貨日期',null=True) cost = models.DecimalField (max_digits=60,decimal_places=2,db_column='退貨價格',null=True) return_volume = models.IntegerField( db_column='退貨數量', null=True) batch = models.IntegerField(db_column='批號',null=True) is_delete = models.BooleanField(default=False, db_column='是否刪除') date_changed = models.DateTimeField(auto_now=True,db_column='最後修改日期') def __str__(self): return self.name class Meta: db_table = "商品退貨表"