【问题标题】:The current path, home/, didn't match any of these当前路径 home/ 与其中任何一个都不匹配
【发布时间】:2020-01-02 06:51:02
【问题描述】:

我的项目中有两个应用程序 - 一个是帐户和家庭 主页应用程序 url - http://localhost:8000/home/ 出现错误, 当前路径 home/ 与其中任何一个都不匹配。

我正在使用 django 版本 2.2.4 我在我的主页 url 模式上使用正则表达式

我的主页应用网址

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.home, name='home')
]

我的主页视图

view.py

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.

def home(request):
    return HttpResponse('It worked !..')

主网址

urls.py

urlpatterns = [

    path(r'^admin/', admin.site.urls),
    url(r'^$', views.login_redirect, name='login_redirect'),  # automatically takes to login page
    path(r'^account/', include(('accounts.urls','accounts'), namespace='accounts')),
    path(r'^home/', include(('home.urls','home'), namespace='home')),
    path('', include('django.contrib.auth.urls')),

] 

我期待浏览器上出现简单的消息“它成功了!..”

【问题讨论】:

  • 它是主应用程序的 urls.py,项目 URL 表示处理所有应用程序 URL 的主 urls.py
  • 删除^ 中的r'^$'

标签: python django path web-frameworks django-2.2


【解决方案1】:

如果您使用的是 django 2.2.4,则无需在 URL 模式中使用正则表达式。改变

path(r'^home/', include(('home.urls','home'), namespace='home'))

path('home/', include('home.urls'))

同样,改变

url(r'^$', views.home, name='home')

path('', views.home,  name = 'home')

有关 URL 调度程序的更多信息,请查看https://docs.djangoproject.com/en/2.2/topics/http/urls/

【讨论】:

    【解决方案2】:

    只需将其更改为

    path('', views.home)
    

    它会起作用的。

    【讨论】:

    • 请让你的答案更清楚,为什么你的代码有效?仅仅说“它会起作用”是不够的
    • 这与接受的答案有何不同?另外:请不要说“简单”——这对那些不觉得简单的人没有帮助。
    猜你喜欢
    • 2020-11-22
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多