Python 3.x 为Django应用程序的所有url显示相同的模板
以下是两种观点:Python 3.x 为Django应用程序的所有url显示相同的模板,python-3.x,django,django-views,django-templates,Python 3.x,Django,Django Views,Django Templates,以下是两种观点: from django.shortcuts import render from django.views import View from django.http import HttpResponse class sectionOfficer_home(View): def get(self, request, *args, **kwargs): return render(request, 'sectionofficer
from django.shortcuts import render
from django.views import View
from django.http import HttpResponse
class sectionOfficer_home(View):
def get(self, request, *args, **kwargs):
return render(request, 'sectionofficer/UI/index.html', {})
class sectionOfficer_main(View):
def get(self, request, *args, **kwargs):
return render(request, 'sectionofficer/UI/404.html', {})
以下是应用程序sectionOfficer的my URL.py:
from django.urls import path
from .views import (sectionOfficer_home, sectionOfficer_main)
app_name= 'sectionofficer'
urlpatterns = [
path('', sectionOfficer_home.as_view(), name = "sectionofc"),
path('secmain', sectionOfficer_main.as_view(), name = "secmain")
]
下面是全局URL.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('sectionoffc', include('sectionofficer.urls')),
path('secmain', include('sectionofficer.urls')),
]
但它始终显示任何不同url的第一个视图模板。这里怎么了?请提供帮助。应用程序
sectionofficer
中的两个URL的名称都是sectionofc
。。。这意味着如果您使用C“%”的{%url”部分
或reverse
或reverse\u lazy
,它们将始终解析为第一个模式。给他们一个不同的名字…仍然不起作用。你是手动键入URL,还是跟随页面的链接?如果您遵循页面中的链接,请显示您为其编写的代码。(如果您更改url名称,自然您必须更改代码中引用的所有url)我正在手动键入url。最好始终使用尾随斜杠结束url模式,您可以直接在此处看到路径('sectionoffc',include('sectionOffer.url'))
用它代替sectionoffc/secmain
你的url会变成sectionoffcsecmain
,这真的让人觉得很奇怪,不是吗?另外,您的其他模式也不会以斜杠结尾,除非您设置了APPEND\u SLASH=False
(不推荐),否则您将获得404。