用户注册功能实现


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


验证表单:

#用户注册表单验证
class RegForm(forms.Form):
    username = forms.CharField(label='用户名',min_length=3,max_length=20,
                               widget=forms.TextInput(attrs={"class": "form-control", 'placeholder': '请输入用户名'}))
    # widget=forms.TextInput 样式
    password1 = forms.CharField(label='密码',min_length=6,max_length=20,
                               widget=forms.PasswordInput(attrs={"class": "form-control", 'placeholder': '请输入密码'}))
    # attrs=可以传递一些参数进去,比如CSS类
    password2 = forms.CharField(label='密码',min_length=6,max_length=20,
                               widget=forms.PasswordInput(attrs={"class": "form-control", 'placeholder': '请再次输入密码'}))
    # attrs=可以传递一些参数进去,比如CSS类
    email = forms.CharField(label='email',
                               widget=forms.EmailInput(attrs={"class": "form-control", 'placeholder': '请输入邮箱'}))
    # attrs=可以传递一些参数进去,比如CSS类
    def clean_username(self):
        username = self.cleaned_data['username']
        #验证用户名是否已经存在
        user = User.objects.filter(username=username).exists() #.exists()方法判断存在就返回True
        if user:
            raise forms.ValidationError("用户名已经存在!")
        return username

    def clean_password2(self):
        password1 = self.cleaned_data['password1']
        password2 = self.cleaned_data['password2']
        # 验证两次密码是否已经存在
        if password1 != password2 :
            raise forms.ValidationError("两次密码输入不一样!请核对重新输入。")
        return password2
    def clean_email(self):
        email = self.cleaned_data['email']
        #验证邮箱是否已经存在
        user_emali = User.objects.filter(email=email).exists() #.exists()方法判断存在就返回True
        if user_emali:
            raise forms.ValidationError("邮箱已经存在!")
        return email

URL:

path('blog_register/', views.blog_register,name='blog_register'),#

视图:

def blog_register(request):
    if request.method == 'POST':
        regform =RegForm(request.POST)  #提交内容去表单验证
        if regform.is_valid(): #如果验证通过,就从表单里取回数据
            username = regform.cleaned_data['username']
            password = regform.cleaned_data['password1']
            email = regform.cleaned_data['email']
            user=User.objects.create_user(username=username,password=password,email=email)
            user.save()
            user_ex = User_EX()
            user_ex.user = User.objects.filter(username=username).first()
            user_ex.bj = 'blog_2018'
            user_ex.save()
            user = auth.authenticate(username=username, password=password)  # 去用户模型里比对
            auth.login(request, user)
            # 重定向
            p_url = reverse('blog_index')
            # 反转
            return redirect(p_url)
    else:
        regform = RegForm()
    context = {
        'regform': regform
              }
    return render(request, 'blog/blog_register.html', context)

HTML模板:

<div class="title">
        <h3 style="line-height: 1.3">晖哥哥博客注册</h3>
      </div>
    <div class="widget widget_search">
        <form class="navbar-form" action="{% url 'blog_register' %}" method="post">
          <div class="input-group">
              {% csrf_token %}
                {% for field in regform %}
                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                    {{ field }}
                    <p>{{ field.errors.as_text }}</p>
                {% endfor %}
                <span>{{ regform.non_field_errors }}</span>
                    <input type="submit" value="注册">
             </div>
        </form>
      </div>

 

点赞

0




登陆后方可评论