Django路径不匹配

时间:2018-04-02 15:15:53

标签: python django path

我的网址有问题。我的站点在localhost中的路径是:

当我想登录时,我会点击按钮,路径如下:

当我提交表单路径时,如下所示:

问题是当我输入错误的密码或用户名时,django会将表单返回到同一个网址:

当我再次输入正确的用户名和密码时,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}) 

上面的行返回/登录,但它返回/ login / logUser,这是错误的

else:
    print("Error: bad request in login.")
    return HttpResponseRedirect('/detska-kniznica')

有什么想法吗?非常感谢你。

1 个答案:

答案 0 :(得分:2)

问题出在这篇HTML中:

<form action="login/logUser" method="post" novalidate>

应该是:

<form action="/login/logUser" method="post" novalidate>

如果您没有领先/,则您的浏览器会认为该网址是相对的。