django2: How to use different templates?

可紊 提交于 2021-02-10 18:29:38

问题


Say I created a django project called django_site.

I created two sub-projects: site1 and polls.

You see that I have two index.html in two sub-projects directories.

However, now, if I open on web browser localhost:8000/site1 or localhost:8000/polls, they all point to the index.html of polls.

How can I configure so when I open localhost:8000/site1 it will use the index.html of site1?

My settings.py in django_site directory:

..
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',
            ],
        },
    },
]

..

My directory structure:

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

My urls.py in django_site

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),
]

My urls.py in site1 or polls (they are the same):

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

回答1:


You have to configure urls like this :

In your django_site directory you have an url file. You have to get urls from all your django apps :

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')),
    ]

Then, inside each django module, you should have :

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

from polls import views

urlpatterns = [
    path('', views.your_function, name="index"),
    ]

and the same thing for site1 :

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

from site1 import views

urlpatterns = [
    path('', views.your_function, name="index"),
    ]

views.your_function have to return your index template : index.html

Your function could be :

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

In this way, you have to get : path('', views.MyFunction, name="index"),




回答2:


I solved the problem by:

In views.py in site1 or polls:

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

And inside django_site I created a folder templates, then in this folder there are two folders site1 and polls. In each subfolder I put index.html respectively.



来源:https://stackoverflow.com/questions/49053579/django2-how-to-use-different-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!