运行 使用 URL 调度程序进入 404 错误

Running into 404 error with URL Dispatcher

这是我在 WebFetcherurl.py 中的

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', include('Fetcher.urls')),
    path('admin/', admin.site.urls),
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是我在 Fetcherurl.py 中的

from django.urls import path

from . import views

urlpatterns = [
    path('', views.home, name = 'home'),
    path('page_objects/', views.page_objects, name = 'page_objects')
]

这是我的表格

<form action="{% url 'page_objects' %}" method="post" enctype="multipart/form-data">

这是我的函数在视图中的名称

def page_objects(request):

我收到 404 错误提示 使用 WebFetcher.urls 中定义的 URLconf,Django 尝试了这些 URL 模式,顺序为:

[姓名='home'] page_ojects [姓名='page_objects'] 行政/ ^媒体/(?P.*)$ 当前路径 WebFetcher/page_ojects 与其中任何一个都不匹配。

我准备了有关 URL Dispatcher 的所有文档,但我找不到任何看起来与我的代码有问题的地方。我希望这只是一个语法错误。如果您认为我的更多代码会有帮助,请发表评论,我将对此进行编辑 post.

编辑 1:

我根据 Daniel 的建议更新了 WebFetcher urls.py 和 Fetcher urls.py。

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from Fetcher import views

urlpatterns = [
    path('WebFetcher/', include('Fetcher.urls')),
    path('', views.home, name = 'home'),
    path('admin/', admin.site.urls),
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)



from django.urls import path

from . import views

urlpatterns = [
    path('page_objects/', views.page_objects, name = 'page_objects')
]

现在我得到的 404 错误是

找不到页面 (404) 请求方法:POST 请求 URL:http://127.0.0.1:8000/WebFetcher/WebFetcher/WebFetcher/page_objects/ 使用 WebFetcher.urls 中定义的 URLconf,Django 尝试了这些 URL 模式,顺序为:

WebFetcher/page_objects/[名称='page_objects'] [姓名='home'] 行政/ ^媒体/(?P.*)$ 当前路径 WebFetcher/WebFetcher/page_objects/ 与其中任何一个都不匹配。

您的 urls.py 路径有错字。

page_ojects 应该是 page_objects

请写下这个可以正常工作的模式。

path('page_ojects/', views.page_objects, name = 'page_objects')

已更新:将此代码添加到您的项目级别 urls.py

 from django.contrib import admin
 from django.urls import path, include

 urlpatterns = [
 path('admin/', admin.site.urls),
 path('', include('Fetcher.urls')),
 ]

如果它不能解决您的问题,那么您的 views.py 模块可能有问题,也许您的视图没有找到任何正确的视图来调度。所以写下面的代码用于测试目的,在你的 views.py

from django.shortcuts import render

def page_objects(request):

return render("Hello, pages_objects!!")

您需要前往 http://mywebsite/page_objects 而不是 http://mywebsite/WebFetcher/page_objects

如果您希望 page_objects url 嵌套在 WebFetcher 下,那么您可以对 urls.py:

MyProject/urls.py:

urlpatterns = [
    path('WebFetcher/', include('Fetcher.urls')), # add the prefix here
    path('admin/', admin.site.urls),
]

Fetcher/urls.py:

urlpatterns = [
    path('', views.home, name = 'home'),
    path('page_objects/', views.page_objects, name = 'page_objects')
]

注意:您的主页现在位于 http://mywebsite/WebFetcher - 如果您希望它位于根目录,即 http://mywebsite,那么您可以这样做:

MyProject/urls.py:

urlpatterns = [
    path('WebFetcher/', include('Fetcher.urls')), # add the prefix here
    path('', views.home, name = 'home'), # move the home page path to the root urls.py
    path('admin/', admin.site.urls),
]

Fetcher/urls.py:

urlpatterns = [
    path('page_objects/', views.page_objects, name = 'page_objects')
]