Django模板文件夹

34
我正在尝试使用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文件? - Brandon Taylor
5个回答

67

你是否在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
  ...

谢谢!我修改了settings.py中的设置,现在一切都按照预期工作了!非常感谢 :) 我仍然发现有些问题与settings.py有关,很难弄清楚。我很高兴它现在能正常工作了。非常感谢! - Kevinvhengst
@KevinvanHengst 没问题,当我开始时也遇到了类似的问题,不得不花很长时间去寻找答案,很高兴我能帮到你。 - Ngenator
感谢您的出色回答。我现在正在做这个,应用程序模板可以工作,但扩展项目模板不行。模板加载器仅在应用程序文件夹中搜索。有什么建议吗? - cbrad
8
这很好,对于django 1.9,您想要更新的变量是位于"TEMPLATES ="中的"DIRS",由于默认项目根目录是BASE_DIR,因此在TEMPLATES字典中应该如下所示:'DIRS': [os.path.join(BASE_DIR,'templates').replace('\','/'),]。 - Ezekiel Kruglick
在Django +1.9中,此方法已被弃用。 - Ali Akhtari

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
我会重新组织URL,如下所示:

我会重新组织URL,如下所示:

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

模式的匹配是根据它们的特定性进行的,因此我倾向于首先放置更具体的模式。否则,您可能会看到一些意外的行为。尝试一下,如果它仍然在请求/时从您的博客加载模板,我们将深入挖掘。


它仍在尝试从blog/templates加载模板。我已经查看了settings.py,如果我在TEMPLATE_DIR方面搞砸了什么,但没有指定一个。所以我仍然很好奇它是如何得到这个路径的。 - Kevinvhengst
我建议设置一个模板目录。 - Brandon Taylor
是的,在“settings.py”中查看“TEMPLATE_DIR”,你会找到一些东西。在Django文档中阅读有关该设置的更多信息,你可能会解决它。 - Erica Xu
@Brandon 在这种情况下,顺序是否重要?他使用 $ 正确终止了根目录的正则表达式,因此只会匹配根 URL /。我知道按照那样的顺序更易读,只是想知道它是否会影响 Django 查找模板的位置。 - Ngenator
两个模式前缺少url,也让我感到困惑哈哈。 - Ngenator
是的,对于你包含的一组模式,实际上没有必要指定URL。我认为问题是由于未指定模板目录引起的。 - Brandon Taylor

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

还有别忘了给你的URL命名空间加上命名空间:https://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接