Error loading admin page after upgrading to django-1.11 - python

So previously, I was using django-1.8 version & I am in the process of upgrading to django-1.11. When I load my /admin page, I get
Exception Type: TemplateDoesNotExist
Exception Value: admin/index.html
I have tried multiple options and dont know why django is not loading admin templates for django-1.11.
Few more details:
- I use django-jet for the admin interface
- My TEMPLATES in settings looks like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'custom_dir_1'),
os.path.join(BASE_DIR, 'custom_dir_2'),
os.path.join(BASE_DIR, 'custom_dir_3')
],
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.request',
'django.template.context_processors.i18n',
'django.contrib.messages.context_processors.messages',
'context_processors.base_context',
],
}
}
]
Let me know if I can provide any other information to debug better. Any guidance in this issue is super appreciated, Have been at this issue for 3rd consecutive day :-(

You must set APP_DIRS to True, i.e.:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'DIRS': [
...

Related

django.template.exceptions.TemplateDoesNotExist: learning_logs\base.html

I'm following along a project from the Python crash course book. I'm supposed to be deploying the webapp to heroku but I keep receiving a Server Error 505 warning.
The problem:
I have tried to do various things with my setings.py file, but none appear to work.
SETTINGS.PY:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [r'\learning_logs\templates\learning_logs',
r'\users\templates\users',],
'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',
],
},
}, ]
Project setup:
learning_log: 11_env learning_log:
.git
learning_log:
....
learning_logs:
templates:
learning_logs:
*all html files including base.html*
...
If anyone requires more information to answer the question, please let
me know.

TemplateDoesNotExist Exception - Django is not loading template from namespaced template

I am using Django 2.2.10
I have an app called myapp, and this is my folder structure:
/path/to/project
...
myapp
...
templates
myapp
index.html
When I place index.html in /path/to/project/myapp/templates/index.html I am able to view the template, however, when I place index.html in the correct subfolder (as shown above) and recommended in the Django documentation (as a way of "namespacing"the templates per application).
I get the eror TemplateNotFound.
This is the relevant portion of my settings.py
settings.py
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',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
],
},
},
]
Why is Django not able to find the template, and how do I fix this?
Make sure, that in 'settings.py' your app name ('myapp') is in 'INSTALLED_APPS' and make sure you are calling template by 'myapp/index.html' in your 'views.py'.

Django 'DIRS': [] in settings.py TEMPLATES causes "TemplateDoesNotExist" Error

I'm using django version 2.1.2 with python 3.6.
I created two django projects (test01 & test02) by CMD.
Both of the projects are under the same folder.
Test01 executes normally, while test02 raises TemplateDoesNotExist error.
I've found a solution for the latter that is hard coding the address of templates in settings.py:
'DIRS': [r'C:\django\test02\accounts\templates']
However, another project can run normally even leaving this list as blank [].
The structures of both projects are the same:
Can anyone give a suggestion that can fix the problem in test02 without hard coding the address of templates in test02?
You may notice a built-in Django variable named BASE_DIR, it represents your root project, so you don't need to hard code the absolute path.
Add this in settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# codes...
],
},
},
]
Register all your apps, and Django will look for any files inside a folder named templates as you mentioned in os.path.join(BASE_DIR, 'templates')
Let create a folder called test01App in template and create base.html on it.
Then you can call test01App/base.html in response.
BACKEND is default of Django and you have to create folder templates.
You can customize where it stores template in other place in DIRS.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, './cuong')],
'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',
],
},
},
]

Django Developments Template_DIRS setting failed

I was recently trying to learn Django for one of my private project.
When came to the Chapter on Template,the Django Book recommended setting template path in settings.py using the the following snippet
TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__),'templates').replace('\\','/'),)
However ,when I opened the file setting.py I found nothing like "TEMPLATE_DIR" but a list:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),],
'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',
],
},
},
]
the the value related to the key "DIR",was a empty list. So I try to filled it with the content shown above.
And then, code something in the views.py(all the import was done )
def current_datetime(request):
now = datetime.datetime.now()
t = get_template("current_datetime.html")
html = t.render(Context({"current_date" : now}))
return HttpResponse(html)
And then mkdir templates in the same folder with setting.py , saved current_datetime.html in folder templates
Finally,run the project.and got the message in my terminal:
WARNINGS:
?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 > and the TEMPLATES dictionary takes precedence. You must put the values of the > following settings into your default TEMPLATES dict: TEMPLATE_DIRS.
System check identified 1 issue (0 silenced).
June 15, 2017 - 15:32:49
Django version 1.11.2, using settings 'mysite.settings'
Starting development server at 127.0.0.1:8000/
Quit the server with CONTROL-C.
When opened the address (127.0.0.1:8000/time/) in my Safari,here came the
error message:
enter image description here
Anyone help,please ??
There is a warning on TEMPLATE_DIRS, for that just be sure you don't have TEMPLATE_DIRS variable in your settings.py and restart development server.
Then for the error, you are actually using a Context object instead of a dict, you should render the template using a useful shortcut https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#example
from django.shortcuts import render
def current_datetime(request):
now = datetime.datetime.now()
return render(request, "current_datetime.html", {
'current_date' : now,
})
I think you have BASE_DIR in settings.py for define location so use like below
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',
],
},
},
DIRS should be like below
'DIRS': [os.path.join(BASE_DIR, 'templates'),],

Django templates and unipath

I'm trying to use unipath module in my django project as it mentioned in Two Scoops of Django book and something works not as intended.
In my settings.py I have this:
from unipath import Path
BASE_DIR = Path(__file__).ancestor(2)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [(BASE_DIR.child('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',
],
},
},
]
STATIC_ROOT = BASE_DIR.child('static')
MEDIA_ROOT = BASE_DIR.child('media')
And with that settings I'm getting TemplateDoesNotExist exception:
Using loader django.template.loaders.filesystem.Loader:
/var/www/(Path('/home/user/my_project/project/templates'),)/myapp/index.html (File does not exist)
What am I doing wrong?
Finally solved this problem!
Need to edit TEMPLATES' 'DIRS' string in settings.py to:
'DIRS': (BASE_DIR.child('templates'),),

Categories

Resources