The current path, **, didn't match any of these(django 2.2.6,page not found)

本文解决了在使用Django 2.2.6版本时遇到的URL配置错误问题,详细解释了path函数的参数作用及正确配置方法。

       近段在学习python(《python 从入门到实战》),由于现有django版本较高,为2.2.6,有很多与书中不一样的地方,在运行中出现了一个错误,如下图:

而learning_logs/urls.py代码如下

from django.urls import path
from . import views

app_name='learning_logs'
urlpatterns = [
    #Home page
    path('',views.index,name='index'),
    path('',views.topics,name='topics'),
]

这里已经添加了topics,而且learning_logs/views.py 也已添加了topics 函数。出现此错误的原因在于path 函数的第一个参数为空。path 函数如下:

path(route,view,[kwargs],[name])

其中前2个参数为必选,后两个可选

(1)path() argument: route

route is a string that contains a URL pattern. When processing a request, Django starts at the first pattern in urlpatterns and makes its way down the list, comparing the requested URL against each pattern until it finds one that matches.

Patterns don’t search GET and POST parameters, or the domain name. For example, in a request to https://www. example.com/myapp/, the URLconf will look for myapp/. In a request to https://www.example.com/ myapp/?page=3, the URLconf will also look for myapp/.

(2)path() argument: view

When Django finds a matching pattern, it calls the specified view function with an HttpRequest object as the first argument and any “captured” values from the route as keyword arguments. We’ll give an example of this in a bit. 

(3)path() argument: kwargs

Arbitrary keyword arguments can be passed in a dictionary to the target view. We aren’t going to use this feature of Django in the tutorial.

(4)path() argument: name

Naming your URL lets you refer to it unambiguously from elsewhere in Django, especially from within templates. This powerful feature allows you to make global changes to the URL patterns of your project while only touching a single file. When you’re comfortable with the basic request and response flow, read part 2 of this tutorial to start working with the database

1、path()参数:route
    route 是一个匹配URL的准则(类似正则表达式)。当Django响应一个请求时,它会从urlpatterns的第一项开始,按顺序依次匹配列表中的项,直到找到匹配的项。
    这些准则不会匹配GET和POST参数或域名。例如,URLconf在处理请求https://www.example.com/myapp/时,它会尝试匹配myapp/。处理请求https://www.example.com/myapp/?page=3 时,也只会尝试匹配 myapp/。
2、path()参数:view
    当 Django 找到了一个匹配的准则,就会调用这个特定的视图函数,并传入一个HttpRequest对象作为第一个参数,被“捕获”的参数以关键字参数的形式传入。
3、path()参数:kwargs
    任意个关键字参数可以作为一个字典传递给目标视图函数。
4、path()参数:name
    为你的URL取名能使你在 Django 的任意地方唯一地引用它,尤其是在模板中。这个有用的特性允许你只改一个文件就能全局地修改某个URL模式。

所以第一个参数为空,只会匹配默认网址,即127.0.0.1:8000/,连127.0.0.1:8000/index也打不开,因为没有url路由,需将learning_logs/urls.py 改为如下:

from django.urls import path
from . import views

app_name='learning_logs'
urlpatterns = [
    #Home page
    path('',views.index,name='index'),
    path('topics/',views.topics,name='topics'),
]

修改后可以正常访问了

Django中出现 “The current path, menu, didn’t match any of these.” 错误,通常意味着请求的URL路径 “menu” 没有在项目的URL配置中找到对应的匹配项。以下是一些可能的解决方案: ### 检查URL配置 确保在项目的 `urls.py` 文件或者应用的 `urls.py` 文件中包含了与 “menu” 路径匹配的URL模式。 例如,在项目的 `urls.py` 中: ```python from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('menu/', include('your_app.urls')), # 假设 'menu' 相关的URL在 'your_app' 中 ] ``` 然后在 `your_app` 的 `urls.py` 中: ```python from django.urls import path from . import views urlpatterns = [ path('', views.menu_view, name='menu'), # 假设 'menu_view' 是处理 'menu' 路径的视图函数 ] ``` ### 检查视图函数 确保对应的视图函数存在且正确。在上面的例子中,`menu_view` 应该在 `your_app/views.py` 中定义: ```python from django.http import HttpResponse def menu_view(request): return HttpResponse("This is the menu page.") ``` ### 检查模板和链接 如果错误是在点击某个链接时出现的,检查模板中的链接是否正确。例如,在模板中应该这样使用链接: ```html <a href="{% url 'menu' %}">Menu</a> ``` ### 重启开发服务器 有时候,修改了URL配置后,需要重启Django开发服务器才能使更改生效。可以使用以下命令重启服务器: ```bash python manage.py runserver ``` ### 检查中间件和路由顺序 确保没有中间件或者路由配置阻止了 “menu” 路径的正常访问。还要注意URL模式的顺序,因为Django会按顺序匹配URL。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值