Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html django中的url路由?_Html_Django_Routing - Fatal编程技术网

Html django中的url路由?

Html django中的url路由?,html,django,routing,Html,Django,Routing,我正在用django创建一个2到3页的网站,所以问题出在html部分,当用html链接页面时,现在有两个页面(索引“网站的主主页-关于”) 因此,当我运行服务器时,它会打开索引页,当我单击关于链接时,url将是(www.xxxx.com/about/about),当我单击索引链接时,url将是(www.xxxx.com/about),而不是索引页。因此,这两个链接将我指向关于页面,但具有不同的url 以下是主项目中的url: urlpatterns = [ path('',include

我正在用django创建一个2到3页的网站,所以问题出在html部分,当用html链接页面时,现在有两个页面(索引“网站的主主页-关于”) 因此,当我运行服务器时,它会打开索引页,当我单击关于链接时,url将是(www.xxxx.com/about/about),当我单击索引链接时,url将是(www.xxxx.com/about),而不是索引页。因此,这两个链接将我指向关于页面,但具有不同的url

以下是主项目中的url:

urlpatterns = [
    path('',include('pages.urls')),
    path('about/',include('pages.urls')),
    path('admin/', admin.site.urls),
] 
以及页面应用程序中的url.py:

urlpatterns = [
    path('',views.index , name='index'),
    path('about/',views.about , name='about'),
]
以及页面应用程序中的views.py:

def index(reqouest):
    return render(reqouest,'pages/index.html')

def about(reqouest):
    return render(reqouest ,'pages/about.html')
以及关于html页面:

<section id="bc" class="mt-3">
    <div class="container">
      <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
          <li class="breadcrumb-item">
            <a href="{% url 'index' %}">
              <i class="fas fa-home"></i> Home</a>
          </li>
          <li class="breadcrumb-item active"> About</li>
        </ol>
      </nav>
    </div>
  </section>

  • 关于
  • p、 s我将html页面放在template/pages文件夹中

    从主应用程序URL中删除
    path('about/',include('pages.url'))

    您不应该有两个包含相同url的url前缀。当您按名称反转url时,只会使用最后一个包含,因为您基本上定义了两次相同的名称

    当您执行
    path('about/',include('pages.url'))
    时,您告诉django在
    页面中构建所有url。url
    前缀为
    about/
    ,因此
    /about/
    成为“索引”url模式,而
    /about/about/
    成为“about”url模式

    第一个
    include
    路径(“”,include('pages.url'))
    )表示您正在创建URL
    /
    /about/
    ,这两个URL都可以工作,但它们不再被命名为URL,因为您用第二个
    include
    覆盖了名称

    因此,如果删除pages.url的第二个
    include
    ,您将得到您想要的。

    remove
    path('about/',include('pages.url')),
    您不应该有两个包含相同url的url前缀。如果这样做,将只使用最后一个include。