【问题标题】:Django templates foldersDjango 模板文件夹
【发布时间】:2013-03-02 21:55:44
【问题描述】:

我正在试验 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')),
)

我是否忽略了什么?

【问题讨论】:

  • 你能发布你的完整 urls.py 吗?

标签: django django-templates django-urls


【解决方案1】:

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

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 文件夹,用于非应用程序模板,每个应用程序本身都有一个 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
  ...

【讨论】:

  • 谢谢!我修改了 setteings.py 中的设置,一切都按预期工作!非常感谢 :) 我仍然很难找出与 settings.py 相关的一些问题。我很高兴它正在工作。非常感谢!
  • @KevinvanHengst 没问题,当我开始时我遇到了类似的问题,不得不去挖掘很长一段时间的答案,很高兴我能提供帮助。
  • 感谢您的出色回答。我现在正在这样做并且应用程序模板可以工作,但扩展项目模板却不行。模板加载器仅在 app 文件夹中进行搜索。有什么建议吗?
  • 这很好,对于 django 1.9,您要更新的变量是位于“TEMPLATES =”中的“DIRS”,并且由于默认项目根目录是 BASE_DIR,它在 TEMPLATES dict 中应该如下所示:' DIRS': [os.path.join(BASE_DIR, 'templates').replace('\\','/'),],
  • 此方法在 Django +1.9 中已弃用。
【解决方案2】:
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"}),
)

【讨论】:

    【解决方案3】:

    我会这样重新组织网址:

    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"}),
    )
    

    模式是根据它们的特异性来匹配的,所以我倾向于把更具体的模式放在第一位。否则,您可能会看到一些意外行为。试一试,如果它仍然在向/ 发出请求时从您的博客加载模板,我们将深入挖掘。

    【讨论】:

    • 它仍在尝试从博客/模板加载模板。如果我用 TEMPLATE_DIR 搞砸了一些东西,我已经查看了 settings.py,但没有指定一个。所以我还是很好奇它是怎么走这条路的。
    • 我建议设置一个模板目录。
    • 是的,查看settings.py 中的TEMPLATE_DIR,你会发现一些东西。在 Django 文档中阅读有关该设置的更多信息,您可能会修复它。
    • @Brandon 在这种情况下排序是否重要?他用$正确地终止了根的正则表达式,这样它只会匹配根url/我知道这样排序会更具可读性,只是想知道它是否真的会影响django在哪里查找模板。跨度>
    • 也被两个模式之前缺少的url弄糊涂了哈哈
    【解决方案4】:

    我认为这取决于您希望您的主页是什么。如果它只是一个链接到您网站其他部分的页面,那么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

    【讨论】:

      【解决方案5】:

      你可以参考我的解决方案

      Django==3.2.5
      

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

      【讨论】:

        猜你喜欢
        • 2018-02-04
        • 2015-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-24
        • 2021-12-27
        • 2018-10-22
        • 1970-01-01
        相关资源
        最近更新 更多