Problem with overriding the default Django admin page.

801 views
Skip to first unread message

Chen Xu

unread,
Jan 17, 2011, 3:25:31 AM1/17/11
to django...@googlegroups.com
Hi, Django group:
I am floowing the tutorial 1 on Django site, which is a poll application
I have problem with overriding the admin page
I copied   admin/base_site.html   from (django/contrib/admin/templates) to   /Users/xuchen81/Django/mysite/admin/base_site.html

and add this line "
/Users/xuchen81/Django/mysite/admin/base_site.html"
to
TEMPLATE_DIRS in my settings.py file. It looks like the following:

TEMPLATE_DIRS = (
    "/Users/xuchen81/Django/mysite/admin/base_site.html",
)

but the admin is just doesn't use this file, it still uses the default base_site.html.

Could anyone please help me?

Thanks

robin nanola

unread,
Jan 17, 2011, 3:51:33 AM1/17/11
to django...@googlegroups.com
try this,

TEMPLATE_DIRS = (
    "/Users/xuchen81/Django/mysite/admin",
)


look  here: http://docs.djangoproject.com/en/dev/ref/settings/?from=olddocs

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Vovk Donets

unread,
Jan 17, 2011, 6:17:18 AM1/17/11
to django...@googlegroups.com
You must specify in the TEMPLATE_DIRS path to the dir where templates were placed, not abs path file
So
TEMPLATE_DIRS = (
    "/Users/xuchen81/Django/mysite/",

)
should work, coz'  "In order to override one or more of them, first create an admin directory in your project's templates directory. This can be any of the directories you specified in TEMPLATE_DIRS."

2011/1/17 Chen Xu <xuch...@gmail.com>
--
Vovk Donets
 python/django developer

skype:  suunbeeam
icq:      232490857
mail:    donets....@gmail.com


Chen Xu

unread,
Jan 17, 2011, 8:26:12 PM1/17/11
to django...@googlegroups.com
I did try this, and I tried again, but it still doesn't work.
Do I need to do something extra like quit the server, and restart again? By the way I did try this too.

Now, I am totally lost.

Thanks

Vovk Donets

unread,
Jan 17, 2011, 9:38:25 PM1/17/11
to django...@googlegroups.com


2011/1/18 Chen Xu <xuch...@gmail.com>

I did try this, and I tried again, but it still doesn't work.
Do I need to do something extra like quit the server, and restart again? By the way I did try this too.

Now, I am totally lost.


Django dev server restarts itself, but sometimes you have to manually restart it.
Well, if you specify paths right and create there is a directory nothing would go wrong:

TEMPLATE_DIRS = (
            "/templates/",
}
and your project for example is here "/var/www/project/" than you go to this dir and in the dir "templates" create dir "admin" and place here thoose admin templates that would replace default admin templates. As i have done it many times if it is done right things works from the start.

for debugging you can delete default admin templates and when Django spills error that template does not exist's look at path's in traceback where django searched templates
it can help to understand problem better

Mike Dewhirst

unread,
Jan 17, 2011, 9:59:55 PM1/17/11
to django...@googlegroups.com
This is what I do ...

in settings.py

# this is the directory containing settings.py
PROJECT_DIR = os.path.realpath(os.path.dirname(__file__))

# if templates are not found here look in app_name/templates
TEMPLATE_DIRS = (os.path.join(PROJECT_DIR, 'templates/'),)

This makes my templates directory a sub-dir of the project dir and
inside that there are other sub-dirs one for each app and one for the admin.

I don't really need one for the admin because django knows where the
admin templates live inside the django tree.

However, there is one admin template I override so that I can
personalise the admin. That is called base_site.html. It is ...

PROJECT_DIR/templates/admin/base_site.html

It is the only admin template I change and it only contains ...

{% extends "admin/base.html" %}
{# admin/base.html is in
site-packages/django/contrib/admin/templates/admin #}
{% load i18n %}
{% block title %}{{ title }} | {% trans 'My site admin' %}{% endblock %}

{% block branding %}
<h1 id="site-name">{% trans 'My administration' %}</h1>
{% endblock %}

Note the comment which indicates that it inherits from the django tree
of admin templates.

How does django know to look at my base_site.html while it is processing
the django tree of templates?

The answer is simple and can be found in your settings.py file here ...

TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)

If the filesystem loader is ahead of the app_directories loader, django
looks in your project templates before its own. Here is the doc
reference ...

http://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types

Hope this helps

Mike


On 18/01/2011 12:26pm, Chen Xu wrote:
> I did try this, and I tried again, but it still doesn't work.
> Do I need to do something extra like quit the server, and restart again?
> By the way I did try this too.
>
> Now, I am totally lost.
>
> Thanks
>
> On Mon, Jan 17, 2011 at 3:17 AM, Vovk Donets <donets....@gmail.com
> <mailto:donets....@gmail.com>> wrote:
>
> You must specify in the TEMPLATE_DIRS path to the dir where
> templates were placed, not abs path file
> So
> TEMPLATE_DIRS = (
> "/Users/xuchen81/Django/mysite/",
>
> )
> should work, coz' "In order to override one or more of them, first
> create an admin directory in your project's templates directory.
> This can be any of the directories you specified in TEMPLATE_DIRS

> <http://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATE_DIRS>."
>
> 2011/1/17 Chen Xu <xuch...@gmail.com <mailto:xuch...@gmail.com>>


>
> Hi, Django group:
> I am floowing the tutorial 1 on Django site, which is a poll
> application
> I have problem with overriding the admin page
> I copied admin/base_site.html from
> (django/contrib/admin/templates) to
> /Users/xuchen81/Django/mysite/admin/base_site.html
>
> and add this line
> "/Users/xuchen81/Django/mysite/admin/base_site.html"
> to TEMPLATE_DIRS in my settings.py file. It looks like the
> following:
>
> TEMPLATE_DIRS = (
> "/Users/xuchen81/Django/mysite/admin/base_site.html",
> )
>
> but the admin is just doesn't use this file, it still uses the
> default base_site.html.
>
> Could anyone please help me?
>
> --

> *Vovk Donets*


> python/django developer
>
> skype: suunbeeam
> icq: 232490857

> mail: donets....@gmail.com <mailto:donets....@gmail.com>


>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com

> <mailto:django...@googlegroups.com>.


> To unsubscribe from this group, send email to
> django-users...@googlegroups.com

> <mailto:django-users%2Bunsu...@googlegroups.com>.

Mike Dewhirst

unread,
Jan 17, 2011, 10:04:05 PM1/17/11
to django...@googlegroups.com
On 18/01/2011 1:59pm, Mike Dewhirst wrote:
> This is what I do ...
>
> in settings.py
>
> # this is the directory containing settings.py
> PROJECT_DIR = os.path.realpath(os.path.dirname(__file__))
>
> # if templates are not found here look in app_name/templates
> TEMPLATE_DIRS = (os.path.join(PROJECT_DIR, 'templates/'),)
>
> This makes my templates directory a sub-dir of the project dir and
> inside that there are other sub-dirs one for each app and one for the
> admin.
>
> I don't really need one for the admin because django knows where the
> admin templates live inside the django tree.
>
> However, there is one admin template I override so that I can
> personalise the admin. That is called base_site.html. It is ...
>
> PROJECT_DIR/templates/admin/base_site.html
>
> It is the only admin template I change


AND it is the only template in my PROJECT_DIR/templates/admin directory.

Jiten Singh

unread,
Jan 18, 2011, 2:46:36 PM1/18/11
to django...@googlegroups.com
Hi,

I am sending my project file details ... Hope this helps

Project Hierarchy

mysite/
   |--->/templates/
   |       |------->/books/(another app like dateapp)
   |       |------>/dateapp/
   |       |          |---->current_datetime.html
   |       |          |---->hours_ahead.html
   |       |-->base.html
   |
   |--->settings.py
--------------------------------------------------------------------
current_datetime.html

{% extends "base.html" %}

{% block title %}The current time{% endblock %}

{% block content %}
<p>It is now {{ current_date }}.</p>
{% endblock %}

---------------------------------------------------------------
my projects settings file TEMPLATE_DIR 

TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)


I my case template inheritance works quite fine. 


It is usually not recommended to meddle with admin app of django. 

I hope instead of giving the path of the file in admin app, if you copy that file (your base.html) in you project's template dir , it may help
330.gif
Reply all
Reply to author
Forward
0 new messages