简体   繁体   English

Django 1.8 BASE_DIR模板问题

[英]Django 1.8 BASE_DIR template issue

I'm new to Django and have encountered an error that really has me stumped. 我是Django的新手,遇到了一个让我难过的错误。 I'm just putting together a basic site and using the tutorial as a bit of a guide, but I've run into an issue regarding templates. 我只是将基本网站放在一起并使用本教程作为指导,但我遇到了有关模板的问题。 I created a templates directory in the same directory as manage.py, created an admin directory within templates, and copied / pasted base_site.html within the admin directory as per the tutorial. 我在manage.py所在的目录中创建了一个templates目录,在模板中创建了一个admin目录,并根据教程在admin目录中复制/粘贴了base_site.html。 I then changed a few of the headers, saved everything, restarted the server, and... nothing. 然后我更改了一些标题,保存了所有内容,重新启动了服务器,并且......没有。 No errors but no changes. 没有错误,但没有变化。 It's as if I had done nothing. 就好像我什么也没做。 I've tried the various fixes suggested on here but none seem to work. 我已经尝试了这里建议的各种修复,但似乎没有工作。 It's as if the templates directory is not being found so the app is using the default. 就像找不到模板目录一样,应用程序正在使用默认值。 Here are the basics that I have right now: 以下是我现在的基础知识:

Directory structure: Project>App, templates>admin>base_site.html, manage.py, etc. 目录结构:项目>应用程序,模板> admin> base_site.html,manage.py等。

In my settings.py: 在我的settings.py中:

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

and

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    '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',
        ],
    },
},

] ]

and in base_site.html: 并在base_site.html中:

<h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('THIS IS WHAT I CHANGED') }}</a></h1>

I'm really hoping someone can point me in the right direction here. 我真的希望有人能指出我正确的方向。

This line: 这一行:

{{ site_header|default:_('THIS IS WHAT I CHANGED') }} 

is evaluating site_header and if found False , will output default. 正在评估site_header ,如果找到False ,将输出默认值。 site_header is a variable defined in AdminSite class. site_headerAdminSite类中定义的变量。 There is no reason from what you describe for site_header to be False , so site_header will be displayed, not the text you changed. 您对site_header描述没有理由为False ,因此将显示site_header ,而不是您更改的文本。

See Default filter description: 请参见Default filter说明

DEFAULT 默认

If value evaluates to False, uses the given default. 如果value的计算结果为False,则使用给定的默认值。 Otherwise, uses the value. 否则,使用该值。

For example: 例如:

{{ value|default:"nothing" }} If value is "" (the empty string), the output will be nothing. {{value | default:“nothing”}}如果value为“”(空字符串),则输出将为空。

Here: https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#default 这里: https//docs.djangoproject.com/en/1.8/ref/templates/builtins/#default

There are ways to change site_header , you can create a custom model based on AdminClass and then define site_header for this new instance. 有一些方法可以更改site_header ,您可以基于AdminClass创建自定义模型,然后为此新实例定义site_header This means defining a new url as well. 这意味着也要定义一个新的URL。

Or you can set site_header: 或者您可以设置site_header:

django.contrib.admin.AdminSite.site_header = "New Header" django.contrib.admin.AdminSite.site_header =“新标题”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM