django项目 crm登录 注册
Posted lnrick
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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):(进行自定义校验)第一种方法
if self.cleaned_data.get(‘password‘) != self.cleaned_data.get(‘re_password‘):
self.add_error(‘re_password‘,‘两次密码不一致‘)
raise ValidationError
return self.clean_data
3,class Meta:(对字段进行配置)
model = models.UserProfile 进行引入
fieds = ‘__all__‘ 全部
fields = [‘username‘,‘password‘ ] 部分
exclude = [‘is_active‘] 除了 这里的is_active,在注册时必须要填写
labels={
‘username‘:‘用户名‘
}
error_messages = {
‘username‘:{
‘required‘:‘必填‘
}
}
widgets = {
‘password‘:froms.widgets.Passwordput(‘class‘:‘form-control‘)
}
def __init__(self,*args,**kwargs)
super().__init__(*args,**kwargs)
for filed in self.fields.values():
filed.widget.attrs.update({‘class‘:‘form-control‘})
两种方法来创建账户:
# form_obj.cleaned_data.pop(‘user_permissions‘)
# form_obj.cleaned_data.pop(‘groups‘)
# form_obj.cleaned_data.pop(‘re_password‘)
# models.UserProfile.objects.create_user(**form_obj.cleaned_data)
user = form_obj.save()
user.set_password(user.password)
user.save()
实例:
admin:
from django.contrib import admin
from crm import models
# Register your models here.
admin.site.register(models.Customer)
admin.site.register(models.ClassList)
admin.site.register(models.Campuses)
froms:
from django import forms
from crm import models
from django.core.exceptions import ValidationError
class RegForm(forms.ModelForm):
re_password = forms.CharField(label=‘确认密码‘,min_length=6, widget=forms.widgets.PasswordInput(attrs={"class": ‘form-control‘}))
def clean(self):
if self.cleaned_data.get(‘password‘) != self.cleaned_data.get(‘re_password‘):
self.add_error(‘re_password‘, ‘两次密码不一致‘)
raise ValidationError(‘两次密码不一致‘)
return self.cleaned_data
class Meta:
model = models.UserProfile
fields = ‘__all__‘
# fields = [‘username‘, ‘password‘]
exclude = [‘is_active‘, ]
labels = {
‘username‘: ‘用户名‘,
‘name‘: ‘真实姓名‘,
‘password‘: ‘密码‘,
# ‘re_password‘: ‘确认密码‘,
}
error_messages = {
‘username‘: {
‘required‘: ‘必填‘,
‘min_length‘: ‘最小长度为6个‘
},
‘re_password‘:{
‘min_length‘: ‘最小长度为6个‘
}
}
widgets = {
‘password‘: forms.widgets.PasswordInput(attrs={"class": ‘form-control‘}),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for filed in self.fields.values():
filed.widget.attrs.update({"class": "form-control"})
views:
from django.shortcuts import render, redirect, HttpResponse
from django.contrib import auth
from crm.forms import RegForm
from crm import models
def login(request):
error_msg = ‘‘
if request.method == ‘POST‘:
username = request.POST.get(‘username‘)
password = request.POST.get(‘password‘)
user = auth.authenticate(request, username=username, password=password)
if user:
auth.login(request, user)
return redirect(‘/index/‘)
else:
error_msg = ‘用户名或密码错误‘
return render(request, ‘login.html‘, {"error_msg": error_msg})
def reg(request):
form_obj = RegForm()
if request.method == ‘POST‘:
form_obj = RegForm(request.POST)
print(form_obj.cleaned_data)
if form_obj.is_valid():
print(form_obj.cleaned_data)
# 保存数据
# form_obj.cleaned_data.pop(‘user_permissions‘)
# form_obj.cleaned_data.pop(‘groups‘)
# form_obj.cleaned_data.pop(‘re_password‘)
# models.UserProfile.objects.create_user(**form_obj.cleaned_data)
# password = form_obj.cleaned_data.get(‘password‘)
user = form_obj.save()
user.set_password(user.password)
user.save()
return redirect(‘/login/‘)
return render(request, ‘reg.html‘, {"form_obj": form_obj})
以上是关于django项目 crm登录 注册的主要内容,如果未能解决你的问题,请参考以下文章
巨蟒django之CRM1 需求分析&&表结构设计&&注册登录验证
Django项目:CRM(客户关系管理系统)--49--40PerfectCRM实现全局账号注册+验证码+页面刷新保留信息