代码之家  ›  专栏  ›  技术社区  ›  dr_colossus

需要帮助修复Django 404错误,使用URLConf找不到静态页面

  •  0
  • dr_colossus  · 技术社区  · 1 年前

    我的Django项目是一个单一的应用程序网站,主页显示我数据库中的文章/帖子,并将有标签主题和年/月档案的页面。所有其他页面都是照片和视频库的静态页面,以及一个关于他们根本不使用数据库的页面。

    我试图显示一个静态页面,但我一直遇到404错误,错误如下(注意:我用“<app_name>”替换了应用程序的名称):

    No article found matching the query
    Request Method: GET
    Request URL:    http://127.0.0.1:8000/illustrations
    Raised by:  <app_name>.views.ArticleDetailView
    Using the URLconf defined in website.urls, Django tried these URL patterns, in this order:
    admin
    [name='home']
    <slug:slug> [name='article_detail']
    The current path, illustrations, matched the last one.
    

    主页显示ListView中的所有文章,您可以单击单个文章在DetailView中以指定的slug作为URL查看它们。“插图”文件是一个单独的静态HTML页面,我将使用它,与数据库或列表或详细视图无关。

    我不知道我是否应该把主页变成一个单独的应用程序,或者我是否只需要更改我的URL/视图文件。(我正在学习Django和Python,所以我肯定会在这里学习。)

    这里 views.py :

        from django.shortcuts import render
        from django.views import generic
        from .models import Article
        from django.http import JsonResponse # Ignore for now, I just put this here for when I work on making a JSON feed
        
        
        class ArticleListView(generic.ListView):
            model = Article
            paginate_by = 6
            template_name = "index.html"
        
        class ArticleDetailView(generic.DetailView):
            model = Article
            template_name = "article.html"
            
        def illustrations(request):
            return render(request, "illustrations.html")
    

    urls.py(项目) :

        from django.contrib import admin
        from django.conf import settings
        from django.urls import path, include
        from django.conf.urls.static import static
        
        urlpatterns = [
            path('admin', admin.site.urls),
            path("", include("<app_name>.urls")),
        ] 
        
        if settings.DEBUG:  # new
            urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    

    urls.py(应用程序) :

        from django.urls import path
        from . import views
        from django.conf.urls.static import static # Not sure if needed
        from django.views.generic import TemplateView
        from .views import ArticleListView, ArticleDetailView
        
        # For setup info, see https://learndjango.com/tutorials/django-file-and-image-uploads-tutorial
        urlpatterns = [
            path("", ArticleListView.as_view(), name="home"), # This is index.html,
            path("<slug:slug>", ArticleDetailView.as_view(), name="article_detail"),
            path("illustrations", views.illustrations, name="illustrations"),
        ]
    
    1 回复  |  直到 1 年前
        1
  •  0
  •   ChrisH    1 年前

    这是因为Django不知道“插图”对你的模型来说不是一个噱头。 slug 类型将匹配任何字母数字字符、连字符或下划线(正则表达式 [A-Za-z0-9-_] .

    Django总是使用第一个匹配的url模式,所以它永远不会到达你想要的“插图”url。

    修复选项:

    1.将“插图”url模式移到ArticleDetailView上方

    # <app>.urls.py
        
    urlpatterns = [
            path("", ArticleListView.as_view(), name="home"), # This is index.html,
            path("illustrations", views.illustrations, name="illustrations"),
            path("<slug:slug>", ArticleDetailView.as_view(), name="article_detail"),
        ]
    

    -注意:你也可以 illustrations 到你的 project.urls 因为这是第一次被泄露。

    2.在DetailView url前添加前缀,使“插图”不匹配

    # <app>.urls.py
        
    urlpatterns = [
        path("", ArticleListView.as_view(), name="home"), # This is index.html,
        path("articles/<slug:slug>", ArticleDetailView.as_view(), name="article_detail"),
        path("illustrations", views.illustrations, name="illustrations"),
        ]
    
    注:

    第二种方法使您更接近于更“传统”的URL/API命名方案。建议您也将ListView更改为“/articles/”。当你只有一个应用程序时,这不是问题,但一旦你添加了另一个(比如“社论”或其他东西),就很容易用“/coditics/”和“/coditions/”复制URL方案。

    保护条款:

    如果你在模板/视图/urls等方面遇到更多问题,请查看django调试工具栏<设置时间为5分钟,比vanilla django提供更多有用的调试信息。

    更多提示:

    如果在URL前添加“app”前缀,请在 project.urls 您导入.urls文件的文件(然后您可以将现有的名称结构保留在/urls b/c中,每个url都将在url前加上“your_app/”前缀。 你没有 拆分url文件,尤其是在一个应用程序中。这并没有错,但当所有url都在一起时,可以更容易地找到东西。