Django模板文件夹

时间:2013-03-14 13:50:14

标签: django django-templates django-urls

我正在尝试使用Django,并弄清楚如何设置 urls.py ,以及URL如何工作。 我在项目的根目录中配置了 urls.py ,以指向我的博客和管理员。 但现在我想在我的家中添加一个页面,所以在localhost:8000

所以我已将以下代码添加到项目根目录中的 urls.py

from django.views.generic.simple import direct_to_template

urlpatterns = patterns('',
    (r"^$", direct_to_template, {"template": "base.html"}),
)

问题是它在 blog / templates /...中搜索模板 而不是我的根目录中的模板文件夹。其中包含 base.html

完整 urls.py

from django.conf.urls import patterns, include, url
from django.views.generic.simple import direct_to_template

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()


urlpatterns = patterns('',
    (r"^$", direct_to_template, {"template": "base.html"}),
    url(r'^blog/', include('hellodjango.blog.urls')),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
    (r'^tinymce/', include('tinymce.urls')),
)

我忽略了什么吗?

5 个答案:

答案 0 :(得分:57)

您是否在TEMPLATE_DIRS中设置了settings.py?检查并确保使用绝对路径正确设置。这就是我确保正确设置的方法:

settings.py

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_ROOT, 'templates').replace('\\','/'),
)

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

这样,我的项目根目录中有一个templates文件夹,用于非app模板,每个app都有一个templates/appname文件夹。

如果您想使用根模板文件夹中的模板,您只需提供模板名称'base.html',如果您想使用应用模板,则使用'appname/base.html'

文件夹结构:

project/
  appname/
    templates/ 
      appname/  <-- another folder with app name so 'appname/base.html' is from here
        base.html
    views.py
    ...

  templates/    <-- root template folder so 'base.html' is from here
    base.html

  settings.py
  views.py
  ...

答案 1 :(得分:4)

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()


urlpatterns = patterns('',
    url(r'^blog/', include('hellodjango.blog.urls')),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^tinymce/', include('tinymce.urls')),
)

urlpatterns += patterns(
    'django.views.generic.simple',
    (r'^', 'direct_to_template', {"template": "base.html"}),
)

答案 2 :(得分:2)

我会重新整理这些网址:

urlpatterns = patterns('',
    (r'^admin/doc/', include('django.contrib.admindocs.urls')),
    (r'^admin/', include(admin.site.urls)),
    (r'^tinymce/', include('tinymce.urls')),
    (r'^blog/', include('hellodjango.blog.urls')),
    (r'^$', direct_to_template, {"template": "base.html"}),
)

模式与其特异性相匹配,因此我倾向于将更具体的模式放在第一位。否则您可能会看到一些意外行为。尝试一下,如果它仍然在您的博客上加载/请求的模板,我们会深入挖掘。

答案 3 :(得分:0)

我认为这取决于你想要的主页。如果它只是一个链接到您网站其他部分的页面,那么catherine's回答是一个很好的干净方式。

如果您希望网站的根目录成为您的博客,我会这样做:

urlpatterns = patterns('',
    # Django Admin
    url(r'^admin/', include(admin.site.urls)),
    # Tiny MCE Urls
    url(r'^tinymce/', include('tinymce.urls')),
    # Other App
    url(r'^other/', include('projectname.other.urls', namespace='other')),
    # Blog App
    url(r'^', include('projectname.blog.urls', namespace='blog')),
)

另外,请不要忘记为您的网址命名空间:https://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces

答案 4 :(得分:0)

你可以参考我的解决方案

Django==3.2.5

https://stackoverflow.com/a/68420097/10852018