Custom django admin templates not working

13,171

Solution 1

Alright I fixed it, this was a stupid mistake but I was already playing with this for the past 2 hours. I had to declare my app before django.contrib.admin. It wouldn't accept it otherwise.

Solution 2

One more mistake that one should resist making on this exercise. The exercise says to change this...

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

to this...

<h1 id="site-name"><a href="{% url 'admin:index' %}">Polls Administration</a></h1>

There is a temptation to only change the string constant, but that is incorrect. I.e. do NOT do this, it will not alter the heading:

<h1 id="site-name"><a href="{% url 'admin:index' %}"> {{ site_header|default:_('Polls Administration') }} </a></h1>

That was the mistake I made, and I had to go through the exercise meticulously to fix it.

Solution 3

I couldn't get the admin template to be recognized when doing this step in part two of the Django tutorial.

This is how I solved it:

Using the information in this answer, I printed the value of TEMPLATE_DIRS:

  1. At the command line, navigate to the directory in which the project's settings.py file exists (for me this is R:\jeffy\programming\sandbox\python\django\tutorial\mysite\mysite\)
  2. Start the interactive shell: python
  3. >>> import settings
  4. >>> settings.TEMPLATE_DIRS, which outputs ['R:\\jeffy\\programming\\sandbox\\python\\django\\tutorial\\mysite\\templates']

So this is the expected location of the templates directory for this project. The admin directory goes into that, and the base_site.html file goes into that.

The other problem I had was that it was working, but I only changed the <TITLE> field, so I just didn't notice it. (I thought I was changing the main header.)

Solution 4

I had this same problem walking through the Django 1.6.5 tutorial (https://docs.djangoproject.com/en/1.8/intro/tutorial02/), but realized it was a mistake in not reading carefully. The tutorial has you do is put this into your settings.py:

TEMPLATES = [
  {  
      ...
     'DIRS': [os.path.join(BASE_DIR, 'templates')],
      ...
  }

I then put the templates folder I wanted to put override for the admin site under the app just like your example:

/project_folder/
      manage.py
      settings.py
      urls.py
      __init__.py
      /app/
          views.py
          models.py
          __init__.py
          /templates/
                /admin/
                    base_site.html

With this it wouldn't work for the reason similar to the issue you had noted by Daniel Roseman in one of the comments, my DIR was evaluating to project_folder/templates (as I told it to). Then I noticed in the tutorial it explicitly said put the templates/admin folder at the project level, not the app level:

Create a templates directory in your project directory (the one that contains manage.py). Templates can live anywhere on your filesystem that Django can access. (Django runs as whatever user your server runs.) However, keeping your templates within the project is a good convention to follow.

By doing this I had the following structure:

/project_folder/
      manage.py
      settings.py
      urls.py
      __init__.py 
      /templates/
          /admin/
              base_site.html
      /app/
          views.py
          models.py
          __init__.py

With this, the everything worked as expected (I could overwrite the default django templates for the admin pages).

While you should be able to put templates anywhere and configure Django to find them, it seems to makes sense to put your main admin templates at the project level, as the admin site's not app specific, but available for the entire project.

Solution 5

Make sure you have set TEMPLATE_DIRS in settings.py to the correct folder!

Share:
13,171
Yonathan
Author by

Yonathan

Security specialist and programmer by trade.

Updated on July 02, 2022

Comments

  • Yonathan
    Yonathan almost 2 years

    I've been trying to get custom templates for the admin page for Django working but have been unsuccessful. I've read the django documentation and several blogs which explain it as being such an easy step, which I assumed it was.

    As of right now the admin page works but my own rewrite of the CSS or templates is not working. My setup is as follows

    /project_folder/
          manage.py
          settings.py
          urls.py
          __init__.py
          /app/
              views.py
              models.py
              __init__.py
              /templates/
                    /admin/
                        base_site.html
    

    In the urls.py I have

    (r'^admin/', include(admin.site.urls)),
    

    Which works since I cannot login etc. So I am assuming the /admin/base_site.html would overwrite the default one but it isn't doing a thing.

    Anyone know what is going on here ? I followed it from the Django tutorials/guides and went onto some blogs to see if they had answers but they all said the same thing.

    Edit 1: I do have my templates directory setup correctly.

    TEMPLATE_DIRS = (
        os.path.join(PROJECT_PATH, 'templates/'),
    )
    

    This works correctly as I have the rest of my site working with a media directory for CSS etc. The only thing not seeming to 'accept' the templates is the admin section.

  • mmdanziger
    mmdanziger almost 10 years
    "The admin directory goes into that" <--This. Easy to overlook. I put the files directly in templates and couldn't figure out why it wasn't working...
  • liquidki
    liquidki almost 10 years
    Just FYI I'm going through the tutorial now with Django 1.6.5 and I didn't have to do this for it to work.
  • liquidki
    liquidki almost 10 years
    I also named the top level directory mysite as well as my app directory mysite, so when I printed TEMPLATE_DIRS and it gave '/Users/username/dev/code/mysite/templates', I was reading too quickly and thought I had it right but I didn't as mine were in .../code/mysite/mysite/templates
  • ed209
    ed209 over 9 years
    I found my problem by breaking the default template (renaming to base_site.htmlx) and the error page showed I had the template folder in the wrong location.
  • jeff
    jeff over 9 years
    We have to do this at the file Projects/mysite/templates/admin/base_site.html right?
  • jeff
    jeff over 9 years
    A side-note, import settings didn't work for me but from mysite import settings did.
  • Rooster
    Rooster about 9 years
    @halilpazarlama yes if you are following the tutorial at docs.djangoproject.com/en/1.7/intro/tutorial02
  • John C
    John C over 7 years
    Glad to see I'm not the only one that had this problem (clearly there's at least 15 others). Using Django 1.10, the docs say things like if you customize the loader, to change your directory order - but nothing about the apps themselves.
  • Carmine Tambascia
    Carmine Tambascia over 6 years
    I can confirm I had same problem and forgot to add in settings.DIRS the path for the templates location.I had difficulty to find out because in another project(but with Django 1.9) the path was setted and still did not work, in this case was because {{ site_header|default:_('Content Manager Interface') }} need to be substitute with a simple text.
  • John Wang
    John Wang about 5 years
    Thanks, that works by removing the site_header|default:... thing. But why cann't I use this context variable since I have not overrided AdminSite Oblect but the template? Are all the original variables gone if I override a template? BTW, I've not touched the AdminSite object, but I do have used my custom ModelAdmin class.
  • djvg
    djvg almost 4 years
    In Django 2.2 my custom templates are loaded regardless of where the app is declared, as long as DIRS is set correctly in settings.TEMPLATES , as mentioned in the documentation.