Custom django admin templates not working

后端 未结 5 1836
星月不相逢
星月不相逢 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: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.

提交回复
热议问题