Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用自定义身份验证的注册视图重定向回注册页面_Python_Html_Django - Fatal编程技术网

Python 使用自定义身份验证的注册视图重定向回注册页面

Python 使用自定义身份验证的注册视图重定向回注册页面,python,html,django,Python,Html,Django,我试图在Django中创建自定义身份验证,其中标识符是电子邮件,有一个名为name的必填字段和一个password字段。登录视图工作正常,但注册视图重定向回同一页面 这是我的观点 def auth_login(request): if request.method == 'POST': email = request.POST['email'] password = request.POST['password'] user = authe

我试图在Django中创建自定义身份验证,其中标识符是电子邮件,有一个名为name的必填字段和一个password字段。登录视图工作正常,但注册视图重定向回同一页面

这是我的观点

def auth_login(request):
    if request.method == 'POST':
        email = request.POST['email']
        password = request.POST['password']
        user = authenticate(email=email, password=password)
        if user is not None:
            login(request, user)
            return HttpResponseRedirect("/tasks/")
        else:           
            return HttpResponse('Invalid login.')
    else:
        form = UserCreationForm()
    return render(request, "registration/login.html", {
        'form': form,
    })

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            new_user = authenticate(email=request.POST['email'], password=request.POST['password1'])
            login(request, new_user)
            return HttpResponseRedirect("/tasks/")
    else:
        form = UserCreationForm()
    return render(request, "registration/register.html", {
        'form': form,
    })
这是我的register.html

<form class="form-signin" role="form" method="post" action="">
    {% csrf_token %}
    <h2 class="form-signin-heading">Create an account</h2>
    <input type="text" name="name" maxlength="30" class="form-control" placeholder="Username" required autofocus>
    <br>
    <input type="email" name="email" class="form-control" placeholder="Email" required>
    <br>
    <input type="password" name="password1" maxlength="4096" class="form-control" placeholder="Password" required>
    <br>
    <input type="password" name="password2" maxlength="4096" class="form-control" placeholder="Password confirmation" required>
    <input type="hidden" name="next" value="/tasks/" />
    <br>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Create the account</button>
</form>

{%csrf_令牌%}
创建帐户




创建帐户
这里怎么了?

而不是

new_user = authenticate(email=request.POST['email'], password=request.POST['password1'])
试一试

new_user = authenticate(email=form.cleaned_data['email'], password=form.cleaned_data['password1'])
答案很好

您需要创建自己的表单,而不是使用django自己的表单 用户创建表单。Django的表单要求您具有用户名

您没有用户名,因此Django的表单不适用于您。 所以创造你自己的。另请参见Django 1.5:UserCreationForm和Custom 验证模型,尤其是答案


那没什么区别。它仍然重定向回注册页。在注册方法中放置一个else语句进行表单验证。表单验证只适用于djangos自己的表单对象。您应该删除验证行或使用django表单