我正在使用自定义视图在Django中创建一个站点,并希望在管理页面上链接到该视图。但即使我按照指示覆盖Django tutorial中的base_site.html
,也没有任何变化。无论输入最简单的改变如何:
{% extends "admin/base.html" %}
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">Test</a></h1>
{% endblock %}
{% block nav-global %}{% endblock %}
甚至是非常激烈的事情,我根本不延伸base.html
:
<h1>Test</h1>
我的目录与它们应该完全一样,其中包含新的base_site.html
:
└─myproject
└── myproject
└── templates
└── admin
└── base_site.html
这是我目前的settings.py
:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = <mysecretkey>
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'easy_thumbnails',
'filer',
'mptt',
'myapp',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'myproject.urls'
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',
],
},
},
]
WSGI_APPLICATION = 'myproject.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
# Thumbnail settings for retina displays
THUMBNAIL_HIGH_RESOLUTION = True
# Thumbnail processing
THUMBNAIL_PROCESSORS = (
'easy_thumbnails.processors.colorspace',
'easy_thumbnails.processors.autocrop',
#'easy_thumbnails.processors.scale_and_crop',
'filer.thumbnail_processors.scale_and_crop_with_subject_location',
'easy_thumbnails.processors.filters',
)
我重新启动了服务器,并使用了不同的浏览器,但我的更改都没有显示出来。
我在任何地方做错了,或者教程给出的说明是否明显不正确?
答案 0 :(得分:8)
Django教程实际上是正确的,并且工作正常。这就是问题:&#34;模板&#34;目录是第一个&#34; mysite&#34;的孩子。直接而不是第二个。
这实际上在教程中非常清楚,但很容易混淆并放置&#34;模板&#34;直接在错误的目录中。
换句话说,你想要&#34;模板&#34;来到这里:
mysite/templates
而不是在这里:
mysite/mysite/templates
Tutorial step #7明确了这一点。这是目录结构的屏幕截图,因为它应该在那个时间点:
只需移动您的&#34;模板&#34;目录上一级,一切都应该很好。
答案 1 :(得分:2)
在将您的示例与我工作的Django 1.10项目进行比较时,我发现了两个不同之处:
my_project
--my_project
----templates
------admin
猜测这并没有任何区别
'DIRS': [join(BASE_DIR, 'my_project/templates').replace ('\\','/'),],
请试一试