2017-02-23 39 views
0

我正在用自定義視圖在Django中創建一個站點,並且想要鏈接到管理頁面上的該視圖。但即使我已按照指示覆蓋Django tutorial中的base_site.html,但沒有任何更改。無論如果輸入簡單的變化:Django 1.10: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', 
) 

我已重新啓動服務器,以及使用不同的瀏覽器,並且我的更改都不顯示。

我在哪裏做錯了什麼,或者是教程給出的指示只是不正確的?

回答

4

Django的教程實際上是正確的,工作正常。問題在於:「templates」目錄直接是第一個「mysite」的子目錄,而不是第二個目錄。

這實際上在教程中已經非常明確,但很容易混淆,並將「模板」直接放在錯誤的目錄中。

enter image description here

換句話說,你想要的 「模板」 在這裏:

mysite/templates 

而不是在這裏:

mysite/mysite/templates 

Tutorial step #7了這一點。這裏的目錄結構的屏幕快照,因爲它應該是在那個時間點:

enter image description here

只需將「模板」目錄上一級,所有應該很好。

1

在您的例子比較對我工作的Django 1.10的項目,我覺得有兩點不同:

  1. 我的文件樹包括對項目文件的應用程序:猜測這並未

my_project --my_project ----templates ------admin

沒有什麼區別

  1. 在TEMPLATES D IRS財產,包括我的名字的模板文件夾的父目錄:

'DIRS': [join(BASE_DIR, 'my_project/templates').replace ('\\','/'),],

請試試這個

+0

哦,我的文件樹是一樣的,我忘了把第一個'我的項目'目錄在樹上。編輯。 – eijen

+0

這有效,但我會稍微等一下,看看是否有一個答案,對內部文件樹的引用較少。 – eijen