我已尝试使用link上的说明,将默认应用模板替换为特定于我网站的模板。
具体来说,我已经设置了以下文件结构:
project_specific_app
-templates
--userena
---files_with_same_names_as_userena_templates.html
TEMPLATE_DIRS:
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.
)
但是,当我尝试更改“files_with_same_name_as_userena_templates.html”的内容并重新启动网络服务器时,网页不会更改
我还忘记了什么?
解决方案:在我的项目/ settings.py中查看TEMPLATE_DIRS并将其更改为/ absolute / path / to / project / specific / app / templates /后,我的自定义模板工作正常。
答案 0 :(得分:2)
可能是因为TEMPLATE_DIRS
中没有定义任何settings.py
。将其修改为:
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.
'full/path/to/your/templates/dir',
)
提示:通常,避免硬编码路径是一种很好的做法。您可以通过这个技巧来获取模板目录的完整路径(或任何需要的路径)并保持项目的可移植性:
import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))
...
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/'),
)
希望这有帮助!