Custom django admin templates not working

后端 未结 5 1760
星月不相逢
星月不相逢 2021-02-05 08:43

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 i

相关标签:
5条回答
  • 2021-02-05 09:23

    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.

    0 讨论(0)
  • 2021-02-05 09:24

    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.)

    0 讨论(0)
  • 2021-02-05 09:25

    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.

    0 讨论(0)
  • 2021-02-05 09:30

    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.

    0 讨论(0)
  • 2021-02-05 09:32

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

    0 讨论(0)
提交回复
热议问题