【问题标题】:Django Userena - Correctly override default django app templates?Django Userena - 正确覆盖默认的 Django 应用程序模板?
【发布时间】:2013-06-05 13:46:55
【问题描述】:

我已经尝试了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”的内容并重新启动网络服务器时,网页并没有改变

我还忘记了什么?

解决方案:在我的 project/settings.py 中查看 TEMPLATE_DIRS 并将其更改为 /absolute/path/to/project/specific/app/templates/ 我的自定义模板有效。

【问题讨论】:

  • settings.py 中的 TEMPLATE_DIRS 变量看起来如何?
  • TEMPLATE_DIRS = ( # 将字符串放在这里,例如 "/home/html/django_templates" 或 "C:/www/django/templates"。# 始终使用正斜杠,即使在 Windows 上也是如此。# Don'不要忘记使用绝对路径,而不是相对路径。)
  • 您不应在问题中发布答案。你宁愿接受给出的答案。

标签: python django web django-templates


【解决方案1】:

可能是因为您没有在settings.py 中定义任何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.
    '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/'),
)

希望这会有所帮助!

【讨论】:

  • 我很高兴,不客气。如果有用,请考虑接受答案:)
猜你喜欢
  • 2012-03-15
  • 2018-06-21
  • 2023-04-03
  • 2019-08-01
  • 2011-04-27
  • 2016-06-04
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多