Admin Site: TemplateDoesNotExist at /admin/

前端 未结 16 1782
鱼传尺愫
鱼传尺愫 2020-12-09 04:00

I\'m following Django\'s official Tutorial 2 but for some reason cannot create an admin site despite following all the steps correctly to my understanding.

This is t

相关标签:
16条回答
  • 2020-12-09 04:27

    I think there are some packages that you didn't install in INSTALLED_APPS, in my case I installed 'import_export' since I used 'importexportmodels

    0 讨论(0)
  • 2020-12-09 04:28
    pip uninstall django
    pip install django --no-cache-dir
    
    

    this two works for me. I'm using django 2.2.3

    0 讨论(0)
  • 2020-12-09 04:29

    In my case, I had to change APP_DIRS from False to True

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]
    
    0 讨论(0)
  • 2020-12-09 04:30

    I ran into similar problem trying to configure django-admin-tools for Django 2.0.2

    Eventually I got it working. Here's my TEMPLATES settings.

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')],
            'APP_DIRS': False,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
                'loaders' : [
                    'django.template.loaders.filesystem.Loader',
                    'django.template.loaders.app_directories.Loader',
                    'admin_tools.template_loaders.Loader',
                    ]
            },
        },
    ]
    

    This worked even as I overrode the default admin template.

    Just be sure to take note of where to place the django-admin-tools apps. See @Abrie Nel's answer above.

    0 讨论(0)
  • 2020-12-09 04:30

    Despite their usefulness, the aforementioned answers are partial. I opine that the developer should know what is going on at the back-end, viz., "how" is this exception being raised.

    As one can see in the exception that the Exception is TemplateDoesNotExist and the value name is the path, and something going wrong with the "loader.py" file. Although I was using Django 1.1.4, I am pretty sure the exception is being raised because of the line (in loader.py) where os.path.isdir(template_dir) is being checked. It cannot find the template folder(directory) in the django folder, which is located in the site-packages folder in the python's Lib folder.

    Intuitively one can say it is because of the inappropriate installation of django. It is, however, a good exercise to find out the cause of the exception by rummaging through the source. When this error occured in my project, I did not reinstall django, instead I copied the folders from root (C:/Python27/django/contrib) -- yes I am using Windows -- to its counterpart in site-packages and it worked!

    0 讨论(0)
  • 2020-12-09 04:31

    Inheriting a Django-application from an experienced magician, I wanted to understand the model by looking at it through Django-admin. As the app's model was not in Admin yet, I created an admin.py and ran into various problems. I tried All-Of-The-Above(TM) and found this:

    1. I got the paths confused. I put the admin.py file into the wrong directory. If you have something like .../app/app/app/app/app... make sure you know which app needs to hold the admin.py. I figured that out by putting crap into the file and notice, that it would not crash. Hence 'crap' was not executed and something was wrong with the file. Fixed.

    2. TemplateDoesNotExist on 'admin/index.html' The app's settings.py was configured to only run django.template.backends.jinja2.Jinja2 as TEMPLATES BACKEND. But Admin needs the standard django.template.backends.django.DjangoTemplates to make it work. I read, that it was possible to have two template-engines configured. I copied the standard-block from a different app. Fixed.

    3. TemplateDoesNotExist on 'admin/widgets/related_widget_wrapper.html' While Admin was now working, looking at individual objects was still broken. I searched for the missing template and found it well in place. Changing anything on my TEMPLATES setting would not improve anything. I manually inserted paths in 'DIRS': [], changed options, deleted the Jinja backend, nothing. It felt like was changing the wrong file again. Using the debugger, I found that even without configuring Jinja, it would still run Jinja. Finally, in settings.py, I spotted this:

      FORM_RENDERER = 'django.forms.renderers.Jinja2'
      

      and commenting it out got Admin going.

    Not sure tho what it does with the rest of the application but for learning it's okay.

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