【问题标题】:Add templates folder in Django 1.8在 Django 1.8 中添加模板文件夹
【发布时间】:2015-09-22 05:55:10
【问题描述】:

我在为我的 django 项目添加模板目录时遇到了一些问题。我的目标是覆盖 /django/contrib/admin/templates/registration/password_reset_form.html 文件。

我的 Django 版本是 1.8。这是我的 settings.py 的模板部分:

 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 PROJECT_PATH = os.path.join(BASE_DIR, 'templates') 
 TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [PROJECT_PATH],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},

]

在我的 django 项目中的模板文件夹中,我添加了修改后的 password_reset_form.html

我的urls.py是这样的:

url(r'^password/reset/$',auth_views.password_reset,name='password_reset'),
url(r'^password/reset/done/$',auth_views.password_reset_done,name='password_reset_done'),
url(r'^password/reset/complete/$',auth_views.password_reset_complete,name='password_reset_complete'),
url(r'^password/reset/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',auth_views.password_reset_confirm,name='password_reset_confirm'),

【问题讨论】:

    标签: python django


    【解决方案1】:

    我总是这样:

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    PROJECT_PATH = os.path.join(BASE_DIR, 'templates') 
    print "Templates in: %s" %(PROJECT_PATH)
    

    以确保我将它们放在正确的位置。那打印什么?另外,发布来自find /path/to/templates 的输出,其中路径是该打印语句的输出。

    【讨论】:

    • Nitpick,但为了兼容 Python 2/3:
    【解决方案2】:

    只需将admin/templates/registration/password_reset_form.html 复制到您的templates 文件夹并在那里覆盖它。您不需要对设置进行任何更改

    【讨论】:

    • 我做到了,但网页没有改变
    猜你喜欢
    • 2016-03-02
    • 2021-12-27
    • 2016-04-02
    • 2021-01-09
    • 1970-01-01
    • 2018-03-18
    • 2013-03-02
    • 2016-02-26
    • 2015-06-19
    相关资源
    最近更新 更多