django项目一 CRM登录注册

逻辑

# 登录
def login(request):
    err_msg = ‘‘
    if request.method == ‘POST‘:
        username = request.POST.get(‘username‘)
        password = request.POST.get(‘password‘)
        obj = auth.authenticate(request, username=username, password=password)
        if obj:
            auth.login(request, obj)
            return redirect(reverse(‘my_customer‘))
        err_msg = ‘用户名或密码错误‘

    return render(request, ‘login.html‘, {‘err_msg‘: err_msg})

# 注册
def reg(request):
    form_obj = RegForm()
    if request.method == ‘POST‘:
        form_obj = RegForm(request.POST)
        if form_obj.is_valid():
            # 创建新用户
            # 方法一
            # form_obj.cleaned_data.pop(‘re_password‘)
            # models.UserProfile.objects.create_user(**form_obj.cleaned_data)

            # 方法二
            obj = form_obj.save()
            obj.set_password(obj.password)
            obj.save()

            return redirect(‘/login/‘)
    return render(request, ‘reg.html‘, {‘form_obj‘: form_obj})

表单处理

# 注册form
class RegForm(forms.ModelForm):
    password = forms.CharField(
        label=‘密码‘,
        widget=forms.widgets.PasswordInput(),
        min_length=6,
        error_messages={‘min_length‘: ‘最小长度为6‘}
    )
    re_password = forms.CharField(
        label=‘确认密码‘,
        widget=forms.widgets.PasswordInput()
    )

    class Meta:
        model = models.UserProfile
        # fields = ‘__all__‘   # 所有字段
        fields = [‘username‘, ‘password‘, ‘re_password‘, ‘name‘, ‘department‘]  # 指定字段
        # exclude = [‘‘]
        widgets = {
            ‘username‘: forms.widgets.EmailInput(attrs={‘class‘: ‘form-control‘}),
            ‘password‘: forms.widgets.PasswordInput,
        }

        labels = {
            ‘username‘: ‘用户名‘,
            ‘password‘: ‘密码‘,
            ‘name‘: ‘姓名‘,
            ‘department‘: ‘部门‘,
        }

        error_messages = {
            ‘password‘: {
                ‘required‘: ‘密码不能为空‘,
            }
        }

    def clean(self):
        pwd = self.cleaned_data.get(‘password‘)
        re_pwd = self.cleaned_data.get(‘re_password‘)
        if pwd == re_pwd:
            return self.cleaned_data
        self.add_error(‘re_password‘, ‘两次密码不一致‘)
        raise ValidationError(‘两次密码不一致‘)

原文地址:https://www.cnblogs.com/niuli1987/p/9857821.html

时间: 2024-10-09 01:01:04

django项目一 CRM登录注册的相关文章

django + vue 简单配置登录注册及分页和实例化

django基本跨域知识点 setting设置 ALLOWED_HOSTS = ['*']??INSTALLED_APPS = [   'django.contrib.admin',   'django.contrib.auth',   'django.contrib.contenttypes',   'django.contrib.sessions',   'django.contrib.messages',   'django.contrib.staticfiles',   'app',  

界面小项目之小米登录注册

<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>登录注册</title> <style> body, ul { margin: 0; padding: 0; list-style: none; } .login { width: 300px; height: 350px; border: 1px solid #aaaaaa; margi

django项目 crm登录 注册

简便添加数据: 在app下的admin下添加表名: admin.site.register(models.Customer)   #前边的的进行注册 admin.site.register(models.ClassList) admin.sit.register(mdoels.Campuses) 然后打开网页输入admin即可, form 的操作: 1,使用forms.ModelForm:(可以取出表的内容) 除了表内的另外的需要自己添加: 2,def clean(self):(进行自定义校验)

Django项目:CRM(客户关系管理系统)--49--40PerfectCRM实现全局账号注册+验证码+页面刷新保留信息

1 # gbacc_urls.py 2 # --------38PerfectCRM实现全局账号登录注销-------- 3 from django.conf.urls import url 4 from gbacc import gbacc_views 5 urlpatterns = [ 6 url(r'^gbacc_login/', gbacc_views.gbacc_login, name='gbacc_login'), # 全局登录 7 # LOGIN_URL = '/gbacc/gba

Django项目:CRM(客户关系管理系统)--70--60PerfectCRM实现CRM学生上课记录

1 #urls.py 2 3 """PerfectCRM URL Configuration 4 5 The `urlpatterns` list routes URLs to views. For more information please see: 6 https://docs.djangoproject.com/en/2.0/topics/http/urls/ 7 Examples: 8 Function views 9 1. Add an import: from

Django项目:CRM(客户关系管理系统)--82--72PerfectCRM实现CRM动态菜单和角色

1 #models.py 2 3 # --------01PerfectCRM基本配置ADMIN-------- 4 5 from django.db import models 6 # Create your models here. 7 8 """ 9 #运行 Terminal 10 # 生成 数据表 11 # python manage.py makemigrations 12 # 数据表 迁移 13 # python manage.py migrate 14 &quo

Django项目:CRM(客户关系管理系统)--49--40PerfectCRM实现全局密码修改

1 # gbacc_urls.py 2 # --------38PerfectCRM实现全局账号登录注销-------- 3 from django.conf.urls import url 4 from gbacc import gbacc_views 5 urlpatterns = [ 6 url(r'^gbacc_login/', gbacc_views.gbacc_login, name='gbacc_login'), # 全局登录 7 # LOGIN_URL = '/gbacc/gba

Django项目:CRM(客户关系管理系统)--61--51PerfectCRM实现CRM客户报名流程学生合同上传照片

1 # sales_views.py 2 # --------47PerfectCRM实现CRM客户报名流程-------- 3 from django.db import IntegrityError #主动捕捉错误信息 4 from django.shortcuts import render #页面返回 5 from crm import models #数据库 6 from bpm.bpm_auxiliary import bpm_forms #自定制 forms 7 8 # -----

Django项目:CRM(客户关系管理系统)--71--61PerfectCRM实现CRM学生上传作业

1 # student_urls.py 2 # --------60PerfectCRM实现CRM学生上课记录-------- 3 from django.conf.urls import url 4 from bpm.student import student_views 5 6 urlpatterns = [ 7 url(r'^student_course/$', student_views.student_course, name='student_course'), #学生报名的课程