错误太多重定向.这页是不是工作现在在django

z2acfund  于 2023-03-04  发布在  Go
关注(0)|答案(1)|浏览(113)

我试图为我的django项目创建一个注册表单html页面.但是我在一个"太多重定向错误"中结束.任何人可以帮助我吗?我是django新手.这里是我的代码:

from . import views
    from django.urls import path
    app_name = 'credentials'
    urlpatterns = [
       path('register/', views.register, name='register'),
       path('login/', views.login, name='login'),
       path('logout/', views.logout, name='logout')
   ]
def register(request):
if request.method == 'POST':
    username = request.POST['username']
    firstname = request.POST['first_name']
    lastname = request.POST['last_name']
    email = request.POST['email']
    password = request.POST['password']
    confirmpassword = request.POST['password2']

    if password == confirmpassword:
        if User.objects.filter(username=username).exists():
            messages.info(request, 'Username Taken')
            return redirect('register')
        elif User.objects.filter(email=email).exists():
            messages.info(request, 'Email Taken')
            return redirect('register')`

        user = User.objects.create_user(username=username, first_name=firstname, last_name=lastname, email=email,
                                        password=password)
        user.save()
        messages.info(request, 'USER REGISTERED')
        print('USER REGISTERED')

else:
    messages.info(request, 'password did not match!!')
    return redirect('credentials:register')
    return redirect('/')
return render(request, 'register.html')
lf3rwulv

lf3rwulv1#

在'else'路径中有2个重定向...第一个创建infinit重定向:

else:
    messages.info(request, 'password did not match!!')
    return redirect('credentials:register')
    return redirect('/')

相关问题