单击链接和页面上的URL编码字符未找到

时间:2018-09-06 01:48:14

标签: python django django-2.1

使用Django 2.1时出现以下错误:

Page not found (404)
Request Method: POST
Request URL:    http://localhost:8000/user-accounts/%5Eregister/
Using the URLconf defined in my_project.urls, Django tried these URL patterns, in this order:

admin/
user-accounts/ ^register/$ [name='register']
user-accounts/ ^login/$ [name='login']
user-accounts/ ^logout/$ [name='logout']
bank-accounts/
The current path, user-accounts/^register/, didn't match any of these.

当我单击base.html中设置的标题中的链接时,在浏览器中得到了奇怪的URL http://localhost:8000/user-accounts/%5Eregister/。 base.html中的链接是:

<li class="dropdown-header"><a href="{% url 'user-accounts:register' %}">Register</a></li>
<li class="dropdown-header"><a href="{% url 'user-accounts:login' %}">Login</a></li>

用户帐户应用urls.py:

from django.urls        import path
from django.contrib     import admin
from .                  import views

app_name = 'user-accounts'
urlpatterns = [
    path(r'^register/$', views.register, name='register'),
    path(r'^login/$', views.user_login, name='login'),
    path(r'^logout/$', views.user_logout, name='logout'),
]

项目urls.py:

from django.contrib import admin
from django.urls import path, include

from user_accounts.views import register

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', register),
    path(r'user-accounts/', include('user_accounts.urls')),
    path(r'bank-accounts/', include('bank_accounts.urls')),
]

1 个答案:

答案 0 :(得分:0)

Django的新(> = 2.0)path()函数不适用于正则表达式。这就是为什么^字符实际上位于URL之前的原因。

如果要继续使用正则表达式,请使用re_path()函数。