Html django中的url路由?
我正在用django创建一个2到3页的网站,所以问题出在html部分,当用html链接页面时,现在有两个页面(索引“网站的主主页-关于”) 因此,当我运行服务器时,它会打开索引页,当我单击关于链接时,url将是(www.xxxx.com/about/about),当我单击索引链接时,url将是(www.xxxx.com/about),而不是索引页。因此,这两个链接将我指向关于页面,但具有不同的url 以下是主项目中的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
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
,您将得到您想要的。removepath('about/',include('pages.url')),
您不应该有两个包含相同url的url前缀。如果这样做,将只使用最后一个include。