当前位置: 首页 > 工具软件 > X2CRM > 使用案例 >

django crm01

云飞翮
2023-12-01

客户管理系统

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

from django.shortcuts import render,HttpResponse,redirect
import os
from django.contrib import auth
from app01.models import UserInfo
from crm import settings
from django.views import View
from django import forms
from app01 import models
from django.utils.decorators import method_decorator
from 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 PageNation
class 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 myform
class 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

 类似资料:

相关阅读

相关文章

相关问答