Ejemplo n.º 1
0
def user_reg(request):
    if request.method == "GET":
        return render(request, 'users/reg.html')
    else:
        user_register_form = UserRegForm(request.POST)
        if user_register_form.is_valid():
            username = user_register_form.cleaned_data['username']
            password = user_register_form.cleaned_data['password']
            email = user_register_form.cleaned_data['email']
            user = UserProfile.objects.filter(username=username)
            is_email = UserProfile.objects.filter(email=email)
            if user:
                return render(request, 'users/reg.html', {'msg': '用户名已存在'})
            elif is_email:
                return render(request, 'users/reg.html', {'msg': '此邮箱已注册'})
            else:
                a = UserProfile()
                a.username = username
                a.email = email
                a.password = password
                a.set_password(password)
                a.save()
                return redirect(reverse('users:login'))
        else:
            return render(request, 'users/reg.html',
                          {'user_register_form': user_register_form})
Ejemplo n.º 2
0
    def test__not_valid_forms(self):
        not_valid_data = {
            'username': '******',
            'email': '*****@*****.**',
        }
        form = UserRegForm(data=not_valid_data)

        self.assertFalse(form.is_valid())
Ejemplo n.º 3
0
def register(request):
    data = UserRegForm(request.POST)  # 验证之后的值
    if not data.is_valid():  # 验证是否合法
        return HttpResponse('erorr')
    res = data.cleaned_data  # 调用is_Valid后会给一个cleaned_data,是合法数据

    User.objects.create_user(**res)
    url_source = request.META['HTTP_REFERER']
    return redirect(url_source)
Ejemplo n.º 4
0
def register(request):
    data = UserRegForm(request.POST)
    if not data.is_valid():
        return HttpResponse('数据不合法')
    res = data.cleaned_data
    # 表里面创建用户
    user = User.objects.create_user(**res)
    user_login(request, user)
    url_source = request.META['HTTP_REFERER']
    return redirect(url_source)
Ejemplo n.º 5
0
    def test_valid_forms(self):
        valid_data = {
            'username': '******',
            'email': '*****@*****.**',
            'password1': 'qwerty212',
            'password2': 'qwerty212',
        }
        form = UserRegForm(data=valid_data)

        self.assertTrue(form.is_valid())