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

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

系统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分钟,在梦中想到这个解决办法的,底层思想就是控制变量法。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/790543.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存