python3.7.4+Django2.2.6一直提示path404报错问题:“Using the URLconf defined in XXX.urls, Django tried this...”
【寫在前面】:
最近在做python+Django做路徑開發(fā)時(shí)一直被路由設(shè)置中的path路徑設(shè)置所困擾,抽空把自己遇到的一些坑的解決方案一起和大家分享一下,歡迎大家評(píng)論區(qū)交流;
【Django應(yīng)用工作原理】:
Django 如何處理一個(gè)請(qǐng)求?
當(dāng)一個(gè)用戶請(qǐng)求Django 站點(diǎn)的一個(gè)頁面,下面是Django 系統(tǒng)決定執(zhí)行哪個(gè)Python 代碼使用的算法:
-
Django determines the root URLconf module to use. Ordinarily, this is the value of the ROOT_URLCONF setting, but if the incoming HttpRequest object has a urlconf attribute (set by middleware), its value will be used in place of the ROOT_URLCONF setting.
-
Django loads that Python module and looks for the variable
urlpatterns. This should be a Python list of django.urls.path()
and/or django.urls.re_path() instances. -
Django依次匹配每個(gè)URL 模式,在與請(qǐng)求的URL 匹配的第一個(gè)模式停下來。
-
Once one of the URL patterns matches, Django imports and calls the given view, which is a simple Python function (or a class-based view). The view gets passed the following arguments:
-
一個(gè) HttpRequest 實(shí)例。
-
If the matched URL pattern returned no named groups, then the matches from the regular expression are provided as positional arguments.
-
The keyword arguments are made up of any named parts matched by the path expression, overridden by any arguments specified in the optional kwargs argument to django.urls.path() or django.urls.re_path().
-
If no URL pattern matches, or if an exception is raised during any point in this process, Django invokes an appropriate error-handling view. See Error handling below.
【path函數(shù)官方說明】:
函數(shù) path() 具有四個(gè)參數(shù),兩個(gè)必須參數(shù):route 和 view,兩個(gè)可選參數(shù):kwargs 和 name。現(xiàn)在,是時(shí)候來研究這些參數(shù)的含義了。 -
path() 參數(shù): route?
route 是一個(gè)匹配 URL 的準(zhǔn)則(類似正則表達(dá)式)。當(dāng) Django 響應(yīng)一個(gè)請(qǐng)求時(shí),它會(huì)從 urlpatterns 的第一項(xiàng)開始,按順序依次匹配列表中的項(xiàng),直到找到匹配的項(xiàng)。
這些準(zhǔn)則不會(huì)匹配 GET 和 POST 參數(shù)或域名。例如,URLconf 在處理請(qǐng)求 https://www.example.com/myapp/時(shí),它會(huì)嘗試匹配 myapp/ 。處理請(qǐng)求 https://www.example.com/myapp/?page=3 時(shí),也只會(huì)嘗試匹配 myapp/。
- path() 參數(shù): view?
當(dāng) Django 找到了一個(gè)匹配的準(zhǔn)則,就會(huì)調(diào)用這個(gè)特定的視圖函數(shù),并傳入一個(gè) HttpRequest 對(duì)象作為第一個(gè)參數(shù),被“捕獲”的參數(shù)以關(guān)鍵字參數(shù)的形式傳入。稍后,我們會(huì)給出一個(gè)例子。
- path() 參數(shù): kwargs?
任意個(gè)關(guān)鍵字參數(shù)可以作為一個(gè)字典傳遞給目標(biāo)視圖函數(shù)。本教程中不會(huì)使用這一特性。
- path() 參數(shù): name?
為你的 URL 取名能使你在 Django 的任意地方唯一地引用它,尤其是在模板中。這個(gè)有用的特性允許你只改一個(gè)文件就能全局地修改某個(gè) URL 模式。
當(dāng)你了解了基本的請(qǐng)求和響應(yīng)流程后,請(qǐng)閱讀 教程的第 2 部分 開始使用數(shù)據(jù)庫.
【錯(cuò)誤提示】:
【解決方案】
- 有問題的path配置
:
urlpatterns = [path('admin/', admin.site.urls),path('myapp/', views.register, name='register'), ]- 修改方法
# path('myapp/', views.register, name='register')# 問題路徑修改為以下兩種方式 path(r'', views.register, name='register') # 修改方式1 path('', views.register, name='register') # 修改方式2通過url路由配置實(shí)現(xiàn)不同網(wǎng)頁之間的切換操作詳見:https://blog.csdn.net/weixin_44322778/article/details/102550161
總結(jié)
以上是生活随笔為你收集整理的python3.7.4+Django2.2.6一直提示path404报错问题:“Using the URLconf defined in XXX.urls, Django tried this...”的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Django(一):项目中urls.py
- 下一篇: websocket python爬虫_p