【问题标题】:URL encoded characters on clicking link and page not found未找到单击链接和页面上的 URL 编码字符
【发布时间】:2018-09-06 01:48:14
【问题描述】:

我在使用 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')),
]

【问题讨论】:

    标签: python django django-2.1


    【解决方案1】:

    Django 的新 (>=2.0) path() 函数不适用于正则表达式。这就是为什么 ^ 字符会直接添加到 URL 之前。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      相关资源
      最近更新 更多