【问题标题】:Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/contacts/contact.html找不到页面 (404) 请求方法:GET 请求 URL:http://127.0.0.1:8000/contacts/contact.html
【发布时间】:2020-07-11 06:36:06
【问题描述】:

我无法将我的 index.html 页面与 contact.html 页面连接起来,当我尝试时它显示了上述错误 使用 Website.urls 中定义的 URLconf,

Django 按以下顺序尝试了这些 URL 模式:

管理员/

[name='主页']

联系方式/

当前路径,contacts/contact.html,与其中任何一个都不匹配。

这是我的contacts.urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('Contact', views.contacts, name='contact-us')
]

我的contacts.views.py

from django.shortcuts import render

# Create your views here.

def contacts(Request):
    return render(Request, 'contact.html')

我的网站.urls.py

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('Websiteapp.urls')),
    path('Contact/', include('contacts.urls'))
]

【问题讨论】:

  • 您已将联系人视图配置为显示在/Contact/Contact

标签: python django django-urls


【解决方案1】:

您的 URL 模式建议您访问 http://127.0.0.1:8000/Contact/Contact

【讨论】:

    【解决方案2】:

    模板应具有从任何template 目录开始的完整路径。

    由于约定是myappname/templates/myappname/,我们将从templates 之后进行剪辑。

    喜欢这个

    def contacts(Request):
        return render(Request, 'myappname/contact.html/')
    

    并且模式应该全部小写,像这样 mywebsite.urls

    from django.contrib import admin
    from django.urls import path, include
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('',include('Websiteapp.urls')),
        path('contact/', include('contacts.urls'))
    ]
    

    contacts.urls

    from django.urls import path
    from . import views
    
    urlpatterns = [
        path('contact', views.contacts, name='contact-us')
    ]
    

    现在从contact/contact访问它

    【讨论】:

      【解决方案3】:

      尝试打开http://127.0.0.1:8000/Contact/ 当您在 urls.py 中定义 /Contact/

      【讨论】:

      • 抱歉不工作
      【解决方案4】:

      DjangoURL dispatcher doc很清楚

      # In settings/urls/main.py
      from django.urls import include, path
      
      urlpatterns = [
          path('<username>/blog/', include('foo.urls.blog')),
      ]
      
      # In foo/urls/blog.py
      from django.urls import path
      from . import views
      
      urlpatterns = [
          path('', views.blog.index),
          path('archive/', views.blog.archive),
      ]
      

      【讨论】:

        猜你喜欢
        • 2016-10-30
        • 2019-08-01
        • 2020-08-15
        • 2023-03-22
        • 2018-07-13
        • 2021-07-25
        • 2021-10-30
        • 2021-07-24
        相关资源
        最近更新 更多