问题描述:大概过程跟下面描述的一样,简单来说就是照着例子学习的时候定制管理页面外观,按照文档要求拷贝了base_site.html文件到templates目录下,并且按照要求修改了settings.py文件之后,模板文件死活不生效的问题。

百度了很久,看到不少遇到这个问题的帖子,但是都没有明确是怎么解决的。

 

解决方法: 文档要求在settings.py后面添加这么一段

mysite/settings.py:

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

但是在settings.py文件中实际上已经定义了TEMPLATE_DIRS了,在后面再加一段这个设置属于重定义,正确的写法应该是找到前面的TEMPLATE=

在后面的DIR属性中增加上面的代码就可以了,不需要单独在settings.py后面加这段完整代码。

代码示例: 

作为一个django新手,例子程序也不写清楚一点,能想到重复定义,并去试了试把TEMPLATE_DIRS 的内容直接改到TEMPLATE里面去也是服了我自己了。不管怎么样,终于解决了一个问题。

 

下面是这个问题的详细描述,在网上百度解决方案时找到的,跟我的情况是一样的。

 

 

I am working through https://docs.djangoproject.com/en/1.7/intro/tutorial02
so far all went fine - but now *templates changes just don't work.*



I think there must be a flaw in that tutorial, something missing,
or something different in django 1.7.1 ?
https://docs.djangoproject.com/en/1.7/intro/tutorial02/#customize-the-admin-look-and-feel


my versions:

python -c "import django; print(django.get_version())"
1.7.1

python --version
Python 2.7.3



*SYMPTOM:*

my changes in
mysite/templates/admin/base_site.html
are simply ignored.



These are my files:

mysite# tree
.
├── db.sqlite3
├── manage.py
├── mysite
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── polls
│ ├── admin.py
│ ├── __init__.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── templates
     └── admin
         └── base_site.html



mysite/settings.py:

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


*Whatever I do, the page*
*http://myserver:8000/admin/polls/question/
<http://myserver:8000/admin/polls/question/>*
*still keeps the old title 'Django administration'*


I want to understand how templates work, because I need them for my real
project.

Thanks a lot!

posted on 2017-03-26 20:24  飞天名猪  阅读(1867)  评论(0编辑  收藏  举报