使用virtualenv时django settings.py中TEMPLATE_DIRS的路径是什么 [英] What is the path for TEMPLATE_DIRS in django settings.py when using virtualenv

查看:32
本文介绍了使用virtualenv时django settings.py中TEMPLATE_DIRS的路径是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 virtualenv,我想知道 settings.py 中的 TEMPLATE_DIRS 应该是什么,例如,如果我在项目的根目录中创建一个模板文件夹文件夹.

I am using virtualenv and I want to know what the TEMPLATE_DIRS in settings.py should be, for example if I make a templates folder in the root of my project folder.

推荐答案

您需要指定模板文件夹的绝对路径.始终使用正斜杠,即使在 Windows 上也是如此.

You need to specify the absolute path to your template folder. Always use forward slashes, even on Windows.

例如,如果您的项目文件夹是/home/djangouser/projects/myproject"(Linux)或C:\projects\myproject\"(Windows),则您的 TEMPLATE_DIRS 如下所示:

For example, if your project folder is "/home/djangouser/projects/myproject" (Linux) or 'C:\projects\myproject\' (Windows), your TEMPLATE_DIRS looks like this:

    # for Linux
    TEMPLATE_DIRS = (
        '/home/djangouser/projects/myproject/templates/',
    )

    # or for Windows; use forward slashes!
    TEMPLATE_DIRS = (
        'C:/projects/myproject/templates/',
    )

或者,您可以使用指定的 PROJECT_ROOT 变量并通过将其与模板文件夹的相对路径连接来生成绝对路径.这样做的好处是,如果您将项目复制到其他位置,您只需更改 PROJECT_ROOT.您需要导入 os 模块才能使其工作:

Alternatively you can use the specified PROJECT_ROOT variable and generate the absolute path by joining it with the relative path to your template folder. This has the advantage that you only need to change your PROJECT_ROOT, if you copy the project to a different location. You need to import the os module to make it work:

# add at the beginning of settings.py
import os

# ...

TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, 'templates/'),
)

这篇关于使用virtualenv时django settings.py中TEMPLATE_DIRS的路径是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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