Django模板路径 [英] Django template Path

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

问题描述

我正在遵循有关 http:// docs的教程。 djangoproject.com/en/dev/intro/tutorial02/#intro-tutorial02 在Windows 7环境中。我的设置文件是:

I'm following the tutorial on http://docs.djangoproject.com/en/dev/intro/tutorial02/#intro-tutorial02 in a Windows 7 environment. My settings file is:

TEMPLATE_DIRS = (
    'C:/django-project/myapp/mytemplates/admin'
)

我得到了 base_template 从Django本身的源代码(django / contrib / admin / templates)中的默认Django管理模板目录中的模板 admin / base_site.html 转换为管理子目录的教程指导的myapp目录。

I got the base_template from the template admin/base_site.html from within the default Django admin template directory in the source code of Django itself (django/contrib/admin/templates) into an admin subdirectory of myapp directory as the tutorial instructed.

似乎没有因为某些原因而生效。任何可能是问题的线索?我必须做一个同步数据库吗?

It doesn't seem to take affect for some reason. Any clue of what might be the problem? Do I have to do a sync db?

推荐答案

我知道这不是在Django教程中,但是最好为路径变量设置相对路径。您可以这样设置:

I know this isn't in the Django tutorial, and shame on them, but it's better to set up relative paths for your path variables. You can set it up like so:

import os

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

...

MEDIA_ROOT = PROJECT_PATH + '/media/'

TEMPLATE_DIRS = (
    PROJECT_PATH + '/templates/',
)

这样你可以移动Django项目和您的路径根将会自动更新。当您设置生产服务器时,这很有用。

This way you can move your Django project and your path roots will update automatically. This is useful when you're setting up your production server.

其次,您的TEMPLATE_DIRS路径有疑问。它应该指向你的模板目录的根目录。此外,它也应该在尾部 / 中结束。

Second, there's something suspect to your TEMPLATE_DIRS path. It should point to the root of your template directory. Also, it should also end in a trailing /.

我只是在这里猜测, code> ... / admin / 目录不是你的模板根。如果你还想写绝对路径,你应该删除对管理模板目录的引用。

I'm just going to guess here that the .../admin/ directory is not your template root. If you still want to write absolute paths you should take out the reference to the admin template directory.

TEMPLATE_DIRS = (
    'C:/django-project/myapp/mytemplates/'
)

应该说,默认情况下,模板加载器应设置为递归遍历到应用程序目录中以查找模板文件。

With that being said, the template loaders by default should be set up to recursively traverse into your app directories to locate template files.

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.load_template_source',
    'django.template.loaders.app_directories.load_template_source',
#     'django.template.loaders.eggs.load_template_source',
)

除非您特意要求,否则不需要复制管理模板覆盖某些东西。

You shouldn't need to copy over the admin templates unless if you specifically want to overwrite something.

如果尚未运行syncdb,则必须运行syncdb。如果您通过runserver托管django,您还需要静态服务您的媒体文件。

You will have to run a syncdb if you haven't run it yet. You'll also need to statically server your media files if you're hosting django through runserver.

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

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