英文:
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 'else' path... the first creates the infinit redirect:
else:
messages.info(request, 'password did not match!!')
return redirect('credentials:register')
return redirect('/')
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论