【小白实战】利用django2.0,搭建属于自己的博客(17)


发布时间:2019-02-15 13:08    作者: Uncle Hui   已过去:1 年,7 月   阅读总量:621 已被赞:0


用户邮件注册及验证功能的实现【下】

我们已经建立了注册链接获取页面,那么现在我们来建立好后台处理注册的视图逻辑!

user/views.py:

用户点击链接,就跳到哪了?

建立对应视图:

#注册
def register(request,code):
    #检查验证码过期没,如果没有就渲染一个注册页面回去
    try:
        code_time = EmailVerifyRecord.objects.get(code=code)
        reg_form = RegisterForm(initial={'email':code_time.email})
        return render(request,'user/zhuce2.html',{'reg_form':reg_form})
    except:
        return render(request,'user/zhuce_guoqi.html')

注册的urls.py

path('register/<code>', register, name='register'),#通过邮箱渲染注册表单

表单处理部分:

【user/forms.py】

class RegisterForm(forms.Form):
    email = forms.EmailField(label='电子邮箱',widget=forms.EmailInput(attrs={'class':'form-control'}))
    nicheng = forms.CharField(max_length=10,min_length=1,label='用户昵称',widget=forms.TextInput(attrs={'class':'form-control'}))
    password = forms.CharField(max_length=20,min_length=8,label='密码',widget=forms.PasswordInput(attrs={'class':'form-control'}))
    password_two = forms.CharField(max_length=20,min_length=8,label='重复密码',widget=forms.PasswordInput(attrs={'class':'form-control'}))

    def clean_email(self):
        email = self.cleaned_data['email']
        user_email = User.objects.filter(email=email).exists()
        if user_email != True:
            return email
        else:
            raise forms.ValidationError('邮箱已经存在,请换一个邮箱吧!')
    def clean_nicheng(self):
        nicheng = self.cleaned_data['nicheng']
        user_nicheng = User.objects.filter(last_name=nicheng).exists()
        if user_nicheng != True:
            return nicheng
        else:
            raise forms.ValidationError('昵称已经存在,请换一个名字吧!')
    def clean(self):
        password = self.cleaned_data.get('password')
        password_two = self.cleaned_data.get('password_two')
        if password == password_two :
            return self.cleaned_data
        else:
            raise forms.ValidationError('两次密码输入不一致,请重新输入!')

# clean _XXX,对某个字段单独验证时候,只能获取到对应字段的值,如要多个字段共同验证,需要使用clean方法。clean _XXX验证谁就返回谁,clean ,需要返回return self.cleaned_data

【uesr/views.py】

def zhuce(request):
    if request.method == 'POST':
        user_reg = RegisterForm(request.POST)
        if user_reg.is_valid():
            email = user_reg.cleaned_data['email']
            nicheng = user_reg.cleaned_data['nicheng']
            password = user_reg.cleaned_data['password']
            user = User()
            user.username = email
            user.email =email
            user.set_password(password)
            user.last_name =nicheng
            user.save()
            #跳到登录页去
            return redirect(reverse('login'))
        else:
            #如果验证不通过,我们就将suer_reg渲染给前端,显示错误提示
            return render(request, 'user/zhuce2.html', {'reg_form': user_reg})
    else:
        #如果不是POST,就提示用户从新注册
        return HttpResponse('请从新注册')

urls.py

path('register/zhuce/', zhuce, name='zhuce'),#通过这个链接获取表单内容,连接处理视图

html 404

<div class="container" >
    <div class="panel panel-primary">
        <div class="panel-heading">尊敬的用户,你的动态注册地址已经失效,请再下面重新申请,如已注册成功,请直接前往登陆。</div>
        <div class="panel-body">
            <form class="form-horizontal" style="width: 60%; margin:0 auto" action="{% url 'get_email_yzm'%}" method="post" >
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">邮箱</label>
                    <div class="col-sm-10">
                        {% csrf_token %}
                       <input type="email" class="form-control" name="email" placeholder="请输入常用电子邮箱注册" required="required" >
                        <button type="submit" class="btn btn-default" >获取邮箱注册链接 </button>
                        <span class="text-danger">{{ message }}</span>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <script src="{% static 'jquery.min.1.12.4.js' %}"></script>
    <script src="{% static 'js/bootstrap.min.js' %}"></script>

点赞

0




登陆后方可评论