Django urls 下划线的坑-Using the URLconf defined in xxx, Django tried these URL patterns, in thi

系统Win11
Python版本 3.10.4
Django版本 2
浏览器:Chrome

在文件名,url配置,view函数名等等,不要随便拼接下划线_,否则在浏览器访问的时候,容易报错。

Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/test_csrf/
Using the URLconf defined in mysite7.urls, Django tried these URL patterns, in this order:

admin/
test_cache
test_mw
test_csrf
abc
The current path, test_csrf/, didn’t match any of these.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
在这里插入图片描述

具体示例:
在这里插入图片描述
图上的命名看起来没有问题对吧,先在去浏览器分别访问一下:
先访问带下划线的test_csrf
在这里插入图片描述
接下来访问无下划线的abc
在这里插入图片描述
至此,我们发现真的不能随便加下划线。
那么,到底是哪里不能加呢,具体是在路由url配置那里。
但是,为了容易记忆,我们命名各自东西,千万随便加下划线,可以改用驼峰命名法等。

在这里插入图片描述

别问我怎么发现这个问题的,我是折腾了几个小时,调了闹钟,睡了25分钟,在梦中想到这个解决办法的,底层思想就是控制变量法。

猜你喜欢

转载自blog.csdn.net/Xidian2850/article/details/124488811
今日推荐