Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.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/8/python-3.x/18.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
django2:如何使用不同的模板?_Django_Python 3.x - Fatal编程技术网

django2:如何使用不同的模板?

django2:如何使用不同的模板?,django,python-3.x,Django,Python 3.x,假设我创建了一个名为django\u site的django项目 我创建了两个子项目:site1和polls 您可以看到,我在两个子项目目录中有两个index.html 但是,现在,如果我在web浏览器上打开localhost:8000/site1或localhost:8000/polls,它们都指向polls的index.html 当我打开localhost:8000/site1它将使用site1的index.html时,如何配置它 我的settings.py位于django_站点目录中: .

假设我创建了一个名为
django\u site
的django项目

我创建了两个子项目:
site1
polls

您可以看到,我在两个子项目目录中有两个
index.html

但是,现在,如果我在web浏览器上打开
localhost:8000/site1
localhost:8000/polls
,它们都指向
polls
index.html

当我打开
localhost:8000/site1
它将使用
site1
index.html
时,如何配置它

我的
settings.py
位于
django_站点
目录中:

..
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'),],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

..
我的目录结构:

E:.
|   db.sqlite3
|   manage.py
|   tree.txt
|   tree1.txt
|   
+---site1
|   |   admin.py
|   |   apps.py
|   |   models.py
|   |   tests.py
|   |   urls.py
|   |   views.py
|   |   __init__.py
|   |   
|   +---migrations
|   |       __init__.py
|   |       
|   +---templates
|   |   \---site1
|   |       \---templates
|   |               index.html
|   |               
|   \---__pycache__
|           models.cpython-36.pyc
|           urls.cpython-36.pyc
|           views.cpython-36.pyc
|           __init__.cpython-36.pyc
|           
+---django_site
|   |   settings.py
|   |   urls.py
|   |   wsgi.py
|   |   __init__.py
|   |   
|   \---__pycache__
|           settings.cpython-36.pyc
|           urls.cpython-36.pyc
|           wsgi.cpython-36.pyc
|           __init__.cpython-36.pyc
|           
\---polls
    |   admin.py
    |   apps.py
    |   models.py
    |   tests.py
    |   urls.py
    |   views.py
    |   __init__.py
    |   
    +---migrations
    |   |   0001_initial.py
    |   |   0002_auto_20180214_0906.py
    |   |   __init__.py
    |   |   
    |   \---__pycache__
    |           0001_initial.cpython-36.pyc
    |           0002_auto_20180214_0906.cpython-36.pyc
    |           __init__.cpython-36.pyc
    |           
    +---static
    |       jquery-3.3.1.min.js
    |       
    +---templates
    |   \---polls
    |       \---templates
    |               index.html
    |               
    \---__pycache__
            admin.cpython-36.pyc
            apps.cpython-36.pyc
            models.cpython-36.pyc
            urls.cpython-36.pyc
            views.cpython-36.pyc
            __init__.cpython-36.pyc
我的
url.py
位于
django\u网站

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

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('site1/', include('site1.urls')),
    path('admin/', admin.site.urls),
]
我的
url.py
位于
site1
polls
(它们是相同的):


您必须像这样配置URL:

在您的
django_站点目录中
有一个url文件。您必须从所有django应用程序获取URL:

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

from polls import views
from site1 import views

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('site1/', include('site1.urls')),
    ]
然后,在每个django模块中,您应该具有:

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

from polls import views

urlpatterns = [
    path('', views.your_function, name="index"),
    ]
对于
site1
,情况也一样:

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

from site1 import views

urlpatterns = [
    path('', views.your_function, name="index"),
    ]
视图。您的_函数必须返回索引模板:
index.html

您的功能可以是:

def MyFunction(request):
    return render(request, 'index.html')

这样,您必须获得:
path(“”,views.MyFunction,name=“index”),
我通过以下方法解决了问题:

views.py中
site1中的

def index(request):
  return render(request, 'polls/index.html', context)

在django_网站内部,我创建了一个文件夹
templates
,然后在这个文件夹中有两个文件夹
site1
polls
。在每个子文件夹中,我分别放置
index.html

你能发布你的URL.py文件吗?我添加了它们。谢谢,请显示您的
索引
视图。在我看来,这不是一个好方法。每个模板都应该设置在good子目录中。
def index(request):
  return render(request, 'polls/index.html', context)