日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python3.7.4+Django2.2.6一直提示path404报错问题:“Using the URLconf defined in XXX.urls, Django tried this...”

發(fā)布時(shí)間:2025/3/20 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3.7.4+Django2.2.6一直提示path404报错问题:“Using the URLconf defined in XXX.urls, Django tried this...” 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【寫在前面】:
最近在做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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。