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

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

问题描述

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



所以我添加了以下代码到urls.py在项目的根目录中:

  from django.views.generic.simple import direct_to_template 

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

pre>

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



完整的urls.py:

 从django.conf.urls导入模式,include,url 
从django.views.generic.simple import direct_to_template

#取消注释下面两行以启用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:)



我是否俯视某事?



提前! :

解决方案

您在<$ c $中设置了 TEMPLATE_DIRS C> settings.py ?检查并确保使用绝对路径正确设置。这是我如何确定正确设置:



settings.py



  PROJECT_ROOT = os.path.abspath(os.path.dirname(__ file__))

TEMPLATE_DIRS =(
#将字符串放在这里,如/ home / html / django_templates或c:/ www / django / templates
#总是使用正斜杠,甚至在Windows上
#不要忘记使用绝对路径,而不是相对路径
os.path .join(PROJECT_ROOT,'templates')。replace('\\','/'),


#知道如何从各种源导入模板的可调用列表。
TEMPLATE_LOADERS =(
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
#'django.template.loaders .eggs.Loader',

这样,我有一个<$ c $在我的项目根目录中使用用于非应用程序模板的c> templates 文件夹,每个应用程序在应用程序本身中都有一个 templates / appname 文件夹。



如果要使用根模板文件夹中的模板,您只需输入模板的名称,如'base.html'如果要使用应用程序模板,则使用'appname / base.html'



文件夹结构:

  project / 
appname /
templates /
appname / ;另一个应用程序名称为appname / base.html的文件夹来自这里
base.html
views.py
...

templates / < - 根模板文件夹所以'base.html'来自这里
base.html

settings.py
views.py
...


So 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.

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

The problem is that it searches for the templete in blog/templates/... Instead of the templates folder in my root. Which contains the base.html

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

EDIT: added complete urls.py :)

Am i overlooking something?

thx in advance! :)

解决方案

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:

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

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.

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'

Folder structure:

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天全站免登陆