注册功能的实现register

1.在urls.py文件里面:

from users.views import RegisterView,

url(r'register/$', RegisterView.as_view(), name='register'),

2.在views.py文件里面:
from .forms import RegisterForm,
from django.contrib.auth.hashers import make_password
 
 
'''注册'''
class RegisterView(View):
def get(self,request):
register_form = RegisterForm()
return render(request,'register.html',{'register_form':register_form})
def post(self,request):
register_form = RegisterForm(request.POST)
if register_form.is_valid():
user_name = request.POST.get('email',None)

if UserProfile.objects.filter(email=user_name):
return render(request, 'register.html', {'register_form':register_form,'msg': '该邮箱已被注册'}, )
pass_word = request.POST.get('password',None)
user_profile = UserProfile()
user_profile.username = user_name
user_profile.email = user_name
user_profile.is_active = False
user_profile.password = make_password(pass_word)
user_profile.save()

# 发送消息
user_message = UserMessage()
user_message.user = user_profile
user_message.message = '欢迎注册'
user_message.save()


send_register_email(user_name,'register')
return render(request,'login.html')
else:
return render(request, 'register.html',{'register_form':register_form},)

这里如果需要用到验证码,请见我的博客里面的验证码功能

3.在forms.py文件里面:(这是需要有验证码的情况)
from captcha.fields import CaptchaField
class RegisterForm(forms.Form):
email = forms.EmailField(required=True)
password = forms.CharField(required=True,max_length=16,min_length=5,)
captcha = CaptchaField(error_messages={'invalid':u'验证码错误'})



3.在template/register.html里面:
在验证码下面添加:
{{ register_form.captcha }} 要是这个样子出错的话,就改用HTML文本:

<img src="/captcha/image/2f3f82e5f7a054bf5caa93b9b0bb6cc308fb7011/" alt="点击获取验证码" class="captcha" /> <input id="id_captcha_0" name="captcha_0" type="hidden" value="2f3f82e5f7a054bf5caa93b9b0bb6cc308fb7011" /> <input autocomplete="off" id="id_captcha_1" name="captcha_1" type="text" />

找到表单数据,在form里面加入: action="{% url 'register' %}" method="post"
在表单结尾之前加入:{% csrf_token %}
注意,表单里面的name属性要和views.py文件里面要传递的属性要相同
判断返回页面的样式,是否显示的是登陆状态,
{% if request.user.is_authenticated %}
...
{% else %}
...
{% endif %}


4.对register.html的post提交的表单经行验证是否合法

在app下面新建一个form.py文件
from django import forms
class RegisterForm(forms.Form):
email = forms.EmailField(required=True)
password = forms.CharField(required=True,max_length=16,min_length=5,)
captcha = CaptchaField(error_messages={'invalid':u'验证码错误'})


5.在register.html里面:
在username的字段经行判断 :
{% if register_form.errors.password %}errorput{% endif %}
新建一个div 来放置报错信息:
{% for key,error in register_form.errors.items%}{{ error }}{% endfor %}{{ msg }}
6.为了用户体验好,我们在form表单里面添加一个value属性   

value='{{ register_form.email.value }}'


发送激活链接:请见我的博客,激活链接



转载于:https://www.cnblogs.com/chenyang13677/p/7771299.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值