【问题标题】:the current path ,About.html didn't match any of these in django当前路径,About.html 与 django 中的任何一个都不匹配
【发布时间】:2018-12-01 19:25:24
【问题描述】:
from django.conf.urls import url
from .import views

urlpatterns = [
    url(r'^$', views.index,name='index'),
    url(r'^About/', views.About,name='About'),
    url(r'^checkout/', views.checkout,name='checkout'),
    url(r'^contact', views.contact,name='contact'),
    url(r'^faqs', views.faqs,name='faqs'),
    url(r'^help', views.help,name='help'),
    url(r'^icons', views.icons,name='icons'),
    url(r'^payment', views.payment,name='payment'),
    url(r'^privacy', views.privacy,name='privacy'),

]

错误信息:

Page not found (404)
Request Method:
GET
Request URL:
http://127.0.0.1:8000/About.html
Using the URLconf defined in shop.urls, Django tried these URL patterns, 
in this order: 
admin/ 
^$ [name='index'] 
^about/$ [name='about'] 
^checkout/$ [name='checkout'] 
^contact/$ [name='contact'] 
^static\/(?P<path>.*)$ 
The current path, About.html, didn't match any of these. 

【问题讨论】:

  • 我不确定您是否已将此 urls 文件包含在您的根 url 文件中,并且您的任何 URL 的末尾都没有 $ 符号。此符号是一个正则表达式,用于匹配字符串的末尾或字符串末尾的换行符之前

标签: django django-models django-forms django-templates django-views


【解决方案1】:

这种错误可能在 2 或 3 种不同的情况下发生。 在您的情况下,您似乎在浏览器地址栏中输入了错误的 URL。

您的正确 URL 应该是 http://127.0.0.1:8000/About(正如您在 URL 模式中所写的那样)。

请记住,About.html - 是您在模板文件夹中创建的 HTML 模板。即使您路由到 html 页面(使用类似字符串:app_name/About.html) - 地址栏中的实际 URL 将根据您在正则表达式路径 r'^url_name' 中写入的内容。如果您在 url 模式中编写 r'^About.html',那么 http://127.0.0.1:8000/About.html 应该可以完美运行。

可能产生此类错误的第二种情况(根据我的经验)是当您忘记在定义 URL 视图的方法内传递“请求”参数时 - 在相应的 views.py 文件中。

你应该有一个名为 About 的方法,它在 views.py 中看起来像这样
def 关于(请求): return render(request,'app_name/About.html')

如果您忘记在 About 的括号中传递参数,则可能会发生这种错误。

最后,如果您使用的是 django 2,请开始使用 re_path 方法来提供正则表达式 url 模式。 url 方法很可能在未来的版本中被弃用。

请参考re_path documentation

【讨论】:

    【解决方案2】:

    您的 URL 不会是 http://127.0.0.1:8000/About.html,而只会是 http://127.0.0.1:8000/about(记住 url 不区分大小写),这将带您进入名为 About 的视图,在您的视图中,您应该在其渲染中引用您的模板( about.html)

    你读过我的第一个 Django 应用 https://docs.djangoproject.com/en/2.0/intro/tutorial01/ 如果你不熟悉 django 的运作方式,这是一个很好的起点

    【讨论】:

      【解决方案3】:

      您尝试点击的不是有效的网址,您必须点击http://127.0.0.1:8000/About 如 urls.py 中所述

      您必须了解 url 和 html 模板之间的区别,此 About.html 将在视图中使用,同时呈现如下:

      return render(request, 'your_app/about.html')
      

      如果你愿意的话,当然可以写一个网址:

      urlpatterns = [
      url(r'^$', views.index,name='index'),
      url(r'^About.html/', views.About,name='About'),
      .
      .
      ]
      

      Check the documentation

      【讨论】:

        【解决方案4】:

        您在url()方法中提供的url不包含任何.html后缀

        你可以直接通过/About进入about页面

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-05-18
          • 2020-11-22
          • 1970-01-01
          • 2020-04-18
          • 2020-01-02
          • 1970-01-01
          • 2021-01-27
          相关资源
          最近更新 更多