错误,重定向次数过多。此页面在Django上无法正常工作。

huangapple go评论67阅读模式
英文:

err too many redirects.this page isnt working now on django

问题

我尝试创建一个用于我的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')

希望这有所帮助。

英文:

i was trying to create a registration form html page for my django project.but i ended up in a "too many redirects error".can anyone help me with this? iam new to django. here's my code :

    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')

</details>


# 答案1
**得分**: 0

你的 'else' 路径中有2个重定向...第一个创建了无限重定向:

```python
else:
   messages.info(request, '密码不匹配!!')
   return redirect('credentials:register')
   return redirect('/')
```

<details>
<summary>英文:</summary>

you have 2 redirects in the &#39;else&#39; path... the first creates the infinit redirect:

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


</details>



huangapple
  • 本文由 发表于 2023年2月24日 02:23:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定