Django 模板文件夹 [英] Django templates folders

查看:42
本文介绍了Django 模板文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm experimenting with Django, and figuring out how to set urls.py, and how the URLs work. I've configured urls.py in the root of the project, to directs to my blog and admin. But now I want to add a page to my home, so at localhost:8000.

因此,我已将以下代码添加到项目根目录中的 urls.py:

So I've added to following code to the urls.py in the root of the project:

from django.views.generic.simple import direct_to_template

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

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

The problem is that it searches for the template in blog/templates/... Instead of the templates folder in my root. Which contains the 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')),
)

我是否忽略了什么?

推荐答案

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

Did you set TEMPLATE_DIRS in your settings.py? Check and make sure it is set up correctly with absolute paths. This is how I make sure it is properly set:

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 文件夹.

This way, I have a templates folder in my project root that is used for non-app templates and each app has a templates/appname folder inside the app itself.

如果您想使用根模板文件夹中的模板,您只需提供模板的名称,例如 'base.html',如果您想使用应用程序模板,您可以使用 <代码>'appname/base.html'

If you want to use a template from the root template folder, you just give the name of the template like 'base.html' and if you want to use an app template, you use '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
  ...

这篇关于Django 模板文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆