博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django crm01
阅读量:6371 次
发布时间:2019-06-23

本文共 6151 字,大约阅读时间需要 20 分钟。

客户管理系统

01 实现登录注册,利用modelform 添加用户

from django.shortcuts import render,HttpResponse,redirectimport osfrom django.contrib import authfrom app01.models import UserInfofrom crm import settingsfrom django.views import Viewfrom django import formsfrom app01 import modelsfrom django.utils.decorators import method_decoratorfrom django.contrib.auth.decorators import login_required# Create your views here.class RegForm(forms.Form):    username = forms.CharField(        label='用户名',        max_length=12,        min_length=6,        error_messages={            'max_length':'用户名不能超过12位',            'min_length':'用户名不能低于6位',            'required':'用户名不能为空',        }    )    password = forms.CharField(        label='密码',        max_length=16,        min_length=6,        error_messages={            'max_length': '密码不能超过12位',            'min_length': '密码不能低于6位',            'required': '密码不能为空',        },        widget=forms.widgets.PasswordInput(render_value=True),    )    r_password = forms.CharField(        label='密码',        max_length=16,        min_length=6,        error_messages={            'max_length': '密码不能超过12位',            'min_length': '密码不能低于6位',            'required': '密码不能为空',        },        widget=forms.widgets.PasswordInput,    )    def __init__(self,*args,**kwargs):        super().__init__(*args,**kwargs)        for field in self.fields:            self.fields[field].widget.attrs.update({
'class':'form-control'})def get_valid_img(request): from PIL import Image #终极版,方式5 import random def get_random_color(): return (random.randint(0,255),random.randint(0,255),random.randint(0,255)) from PIL import Image,ImageDraw,ImageFont img_obj = Image.new('RGB', (236, 34), get_random_color()) #图片对象 draw_obj = ImageDraw.Draw(img_obj) #通过图片对象生成一个画笔对象 font_path = os.path.join(settings.BASE_DIR,'statics/font/ziti.TTF') #获取字体,注意有些字体文件不能识别数字,所以如果数字显示不出来,就换个字体 font_obj = ImageFont.truetype(font_path,16) #创建字体对象 sum_str = '' #这个数据就是用户需要输入的验证码的内容 for i in range(6): a = random.choice([str(random.randint(0,9)), chr(random.randint(97,122)), chr(random.randint(65,90))]) #4 a 5 D 6 S sum_str += a print(sum_str) draw_obj.text((84,10),sum_str,fill=get_random_color(),font=font_obj) #通过画笔对象,添加文字 width=236 height=34 # 添加噪线 for i in range(5): #添加了5条线 #一个坐标表示一个点,两个点就可以连成一条线 x1=random.randint(0,width) x2=random.randint(0,width) y1=random.randint(0,height) y2=random.randint(0,height) draw_obj.line((x1,y1,x2,y2),fill=get_random_color()) # # 添加噪点 for i in range(10): #这是添加点,50个点 draw_obj.point([random.randint(0, width), random.randint(0, height)], fill=get_random_color()) #下面是添加很小的弧线,看上去类似于一个点,50个小弧线 x = random.randint(0, width) y = random.randint(0, height) draw_obj.arc((x, y, x + 4, y + 4), 0, 90, fill=get_random_color()) #x, y是弧线的起始点位置,x + 4, y + 4是弧线的结束点位置 from io import BytesIO f = BytesIO() #操作内存的把手 img_obj.save(f,'png') # data = f.getvalue() # 存这个验证码的方式1:赋值给全局变量的简单测试 # global valid_str # valid_str = sum_str # 方式2:将验证码存在各用户自己的session中,session的应用其实还有很多 request.session['valid_str'] = sum_str return HttpResponse(data)class Login(View): def get(self,request): return render(request,'login.html') def post(self,request): data = request.POST if data.get('code') == request.session['valid_str']: user_obj = auth.authenticate(username=data.get('username'),password=data.get('password')) if user_obj: auth.login(request,user_obj) return redirect('index') else: return redirect('login') else: return render(request,'login.html')class Register(View): def get(self, request): form_obj = RegForm() return render(request, 'register.html',{
"form_obj":form_obj}) def post(self,request): data = request.POST form_obj = RegForm(data) code = data.get('code') error = '' if code == request.session['valid_str']: if data.get('password') == data.get('r_password'): if form_obj.is_valid(): user_obj = UserInfo.objects.create_user(username=data.get('username'),password=data.get('password')) return redirect('login') else: erro = '两次密码不一致' else: erro = '验证码错误' return render(request, 'register.html',{
"form_obj":form_obj,'error':erro})#from app01.page import PageNationclass Index(View): @method_decorator(login_required()) def dispatch(self, request, *args, **kwargs): ret = super(Index, self).dispatch(request,*args,**kwargs) return ret def get(self,request): all_costomer = models.Customer.objects.all() #self,base_url,current_page_num,total_counts,per_page_counts=10,page_number=5 costomer_obj = PageNation(request.path,request.GET.get('page'),all_costomer.count()) start_num = costomer_obj.start_num end_num = costomer_obj.end_num page_html = costomer_obj.page_html() # print(page_html) costomers = all_costomer[start_num:end_num] return render(request,'index.html',{
'all_costomer':costomers,'page_html':page_html})# @login_required()# def index(request):# return render(request,'index.html')class Logout(View): def get(self,request): auth.logout(request) return redirect('login')class Start(View): def get(self,request): return render(request,'starter.html')from app01 import myformclass Addcostomer(View): def get(self,request): customerform = myform.CustomerForm() return render(request,'addcustomer.html',{
'customerform':customerform}) def post(self,request): data = request.POST customerform = myform.CustomerForm(data) if customerform.is_valid(): customerform.save() return redirect('index') else: return render(request,'index.html',{
'customerform':customerform})

 

转载于:https://www.cnblogs.com/zhangjian0092/p/11005641.html

你可能感兴趣的文章
Zen Cart 如何添加地址栏上的小图标
查看>>
SecureCrt 连接Redhat linux
查看>>
[NHibernate]持久化类(Persistent Classes)
查看>>
如何在Hive中使用Json格式数据
查看>>
linux如何恢复被删除的热文件
查看>>
Eclipse(MyEclipse) 自动补全
查看>>
Struts2中dispatcher与redirect的区别
查看>>
zabbix agentd configure
查看>>
地图点聚合优化方案
查看>>
Google Chrome 快捷方式
查看>>
备考PMP心得体会
查看>>
vue proxy匹配规则
查看>>
线上应用故障排查之一:高CPU占用
查看>>
Extend Volume 操作 - 每天5分钟玩转 OpenStack(56)
查看>>
IronPython教程
查看>>
squid via检测转发循环
查看>>
计算分页
查看>>
iptables 做nat路由器脚本
查看>>
数据结构(C语言版)第三章:栈和队列
查看>>
Stopping and/or Restarting an embedded Jetty in...
查看>>