用户注册
一、需求分析
- 1、用户注册包括的获取用户名及密码
- 2、防止用户频繁的注册,需要使用验证码,邮件激活该用户
- 3、需要先判断用户是否已经存在
- 4、对用户提交的密码进行加密处理
二、实现代码
1、导包(导入密码加密的包)
from django.contrib.auth.hashers import make_password
2、视图类的书写
from users.forms import RegisterForm from users import models class RegisterView(View): def get(self, request, *args, **kwargs): register_form = RegisterForm() # 把数据返回给前端 return render(request, 'register.html', {'register_form': register_form}) def post(self, request, *args, **kwargs): register_form = RegisterForm(request.POST) if register_form.is_valid(): username = register_form.cleaned_data.get('email', None) password = register_form.cleaned_data.get('password', None) new_password = make_password(password=password) # 密码加密存入数据库中 result = models.UserProfileModel.objects.create(username=username, email=username, password=new_password) if result: return redirect('home')