Django 1.7 - 更新base_site.html无法正常工作

时间:2014-09-08 15:24:21

标签: python django django-templates django-admin

我正在关注django 1.7的教程(再次)。我无法让管理网站更新。我跟着这个:

Django: Overrideing base_site.html

这样:

Custom base_site.html not working in Django

和几个异地事物链接。

我的设置文件如下所示:

""" Django settings for website project.

For more information on this file, see https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see https://docs.djangoproject.com/en/1.7/ref/settings/ """

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = ''

# SECURITY WARNING: don't run with debug turned on in production! DEBUG = True

TEMPLATE_DEBUG = True

TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')] 

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',   'blog',

)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware', )

ROOT_URLCONF = 'website.urls'

WSGI_APPLICATION = 'website.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '',         'USER': 'root',         'PASSWORD': '',         'HOST': '127.0.0.1',        'PORT': '3306',
    } }

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'GMT'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

我知道我的文件结构正常工作,因为如果我从base_site.html中删除所有内容并将其替换为'wtf',这正是我访问管理站点时显示的内容。我已经从django安装中删除了admin / base_site.html但仍然得到了'Django管理'。

当它没有说'wtf'时,我的base_site.html看起来像这样:

{% extends "admin/base.html" %}

{% block title %}{{ title }} | {{ site_title|default:_('whatever site admin') }}{% endblock %}

{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('whatever site administration') }}</a></h1>
{% endblock %}

{% block nav-global %}{% endblock %}

我想这一定与1.7有关,因为我在1.6中工作但是我已经检查了1.6,1.7和dev的文档并且找不到什么错误。

我正在运行本地MySQL数据库的虚拟环境中开发Windows。

1 个答案:

答案 0 :(得分:15)

首先,我不确定是否是复制/粘贴问题,或者您是否实际注释了TEMPLATE_DIRS。它需要是一个非注释的行:

TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]

至于真正的问题,您必须更换更多模板才能使其正常运行,因为此处定义了site_title:https://github.com/django/django/blob/1.7/django/contrib/admin/sites.py#L36 和site_header在此处定义:https://github.com/django/django/blob/1.7/django/contrib/admin/sites.py#L39

默认只有在不存在时才有效,因此您的模板应如下所示:

{% extends "admin/base.html" %}

{% block title %}{{ title }} | whatever site admin{% endblock %}

{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">whatever site administration</a></h1>
{% endblock %}

{% block nav-global %}{% endblock %}

您可以在此处详细了解默认代码:https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#default