我的URL有问题。我在Local主持的网站的路径是:
- http://127.0.0.1:8000/detska-kniznica
当我想登录时,我将单击按钮,路径看起来像这样:
- http://127.0.0.1:8000/detska-kniznica/login
当我提交表单路径时,如下所示:
- http://127.0.0.1:8000/detska-kniznica/login/loguser
问题是当我键入不正确的密码或用户名django返回同一URL的表单:
- http://127.0.0.1:8000/detska-kniznica/login/loguser
,当我再次键入正确的用户名和密码URL时,看起来像这样:
- http://127.0.0.0.1:8000/detska-kniznica/login/login/loguser,此URL不存在。
我将向您展示我的URL:
urlpatterns = [
path('detska-kniznica', include('homepage.urls')),
path('detska-kniznica/admin', admin.site.urls),
path('detska-kniznica/games', include('games.urls')),
path('detska-kniznica/register', include('register.urls')),
path('detska-kniznica/login', include('login.urls')),
]
和
urlpatterns = [
path('', views.index),
path('/logUser', views.log_user),
path('/logOut', views.log_out_user),
]
我的html提交按钮执行此操作:
<form action="login/logUser" method="post" novalidate>
和我的Views.py看起来像这样:
elif request.method == 'POST' and 'btn_login' in request.POST:
form = inputForms.LogUser(request.POST)
if form.is_valid():
print(form.errors)
user_name = form.cleaned_data['username']
user_password = form.cleaned_data['password']
user = authenticate(username=user_name, password=user_password)
if user is not None:
print("som tu")
request.session.set_expiry(86400) # sets the exp. value of the session
login(request, user) # the user is now logged in
else:
print("help")
print(form.errors)
return render(request, "login.html", {'LogUser': form})
return HttpResponseRedirect('/detska-kniznica')
else:
print(form.errors)
return render(request, "login.html", {'LogUser': form})
线上返回/登录线上方,但它返回/登录/徽标,这是错误的
else:
print("Error: bad request in login.")
return HttpResponseRedirect('/detska-kniznica')
有什么想法吗?非常感谢。
问题在这部分HTML中:
<form action="login/logUser" method="post" novalidate>
应该是:
<form action="/login/logUser" method="post" novalidate>
如果您没有领先的/
,则浏览器假定URL是相对的。