right now in my Django settings file for my project i am trying to build a path to so I can display my templates/projects/index.html file. Can I get help solving this?
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
and in my TEMPLATES dictionary I have this is DIRS
`TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'projects/templates'),
],`
I answered this by updating the Templates.Dirs to:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR / 'templates',
'projects/templates',
],
in django3.2 I was trying this to uses for locating and loading templates?
but doesn't work with me
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ` [BASE_DIR / 'templates']`,
}
default setting was like :
`from pathlib import Path`
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Any clue of what might be the problem?
try using
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
}
if you want to change your template path, you can using this in your settings.py, for example i have a "templates" directory for my templates in root of project:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
},
]
Try this edit in your Django settings.py
TEMPLATES_DIR=BASE_DIR / 'templates'(line-17)
'DIRS':[TEMPLATES_DIR,],(line-58)
I need to find Django TEMPLATES location to add in a line under 'context_processors' om 'OPTIONS'
I researched and found this which looks to be my problem solver, however I am unable to find the location of the document where I am supposed to input the details specified: django.core.exceptions.ImproperlyConfigured: Enable 'django.contrib.auth.context_processors.auth'
The project only relies on Django as a plugin so I am in no means experienced with setting up Django.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
"django.contrib.auth.context_processors.auth",
]
}
}
]
I can
not find the 'TEMPLATES' document.
You put all project configurations in settings.py right at the top of the project
I started a new project and am getting:
django.core.exceptions.ImproperlyConfigured: Enable 'django.contrib.auth.context_processors.auth' in your TEMPLATES setting in order to use the admin application.
I followed the django docs for 1.9:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
}
]
What could be the issue (how does it want me configure)? Thank you
You need to add it into context_processors list in the OPTIONS:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
"django.contrib.auth.context_processors.auth",
]
}
}
]
I'm using django rest framework, and as discribed here : django rest framework doc I've added /rest_framework/api.html in my templates dir.
The structure now is :
|
|\
| apps
| \
| settings.py
\
templates
\
rest_framework
\
api.html
api.html :
{% extends "rest_framework/base.html" %}
{% block footer %}
Hello !
{% endblock %}
settings.py :
...
TEMPLATE_LOADERS = (
('django.template.loaders.cached.Loader', (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader',
)),
)
...
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.markup',
'django.contrib.webdesign',
...
'rest_framework',
...
)
...
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
'PAGINATE_BY': 10
}
The modification I do in api.html are not displayed in the browsable api. What am I doing wrong ?
Are you missing the DIRS from the main settings.py (this tells us where to look for templates (override templates):
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
}
djangorestframework==3.5.x
I had the exact problem where the template was not picked up where the template existed in one of my project app directories, as such:
Project Structure
project/
app1/
templates/
app1/
...
rest_framework/
app.html
settings.py
DEBUG = True
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
],
'debug': DEBUG
},
},
]
I had to follow joao figueiredo's comment, and add a specific template folder outside of the app directory.
Project Structure
project/
app1/
templates/
app1/
...
templates/ # Move your file to a specific template dir
rest_framework/
app.html
settings.py
DEBUG = True
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], # look in this specific folder
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
],
'debug': DEBUG
},
},
]
There is an easier solution. Simply put 'rest_framework' at the bottom of your INSTALLED_APPS list. Or at least after the app where you are overriding the api template. This way, django will go to your app's templates folder searching for api.html template before going to rest_framework tempalte folder.
Let's call this app 'myapi'. In my case I have:
Project Structure
project/
myapi/
templates/
api/
...
rest_framework/
app.html
settings.py
INSTALLED_APPS = (
...
'myapi',
'rest_framework'
)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
],
'debug': DEBUG
},
},
]
Which version of Django REST Framework are you using?
I made changes to the block footer in the base.html and this was planed for the 3.0 release.
Is your 'Hello !' also not showing in the source code of the page (you can get it by pressing CTRL+U)?
If yes, than it could eventually be an issue with CSS making the colour white. You can put 'Hello !' in a tag like this: <p>Hello !</p>.
EDIT:
Additional info.
There was an issue with the sticky footer displaying always 60px below the page bottom, thus scrolling down was needed to see it. If you are using an older version this can be also causing the problem.
The most important question is: is 'Hello !' not at all in the source HTML sent to the browser or is it there, but you can't see it on the page?
Please give me a feedback, so we can solve this.
It is crucial in Django >= 1.8 that you also add "APP_DIRS" to true in the TEMPLATES dictionary.
TEMPLATES = [ {
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'DIRS': [BASE_DIR,os.path.join(BASE_DIR, 'templates')],
... }
you may change your tempaltes settings to
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR,os.path.join(BASE_DIR, 'templates')],
...
}
if you use django1.8,that because it load tempalte diffrently,the BASE_DIR is for your templates,os.path.join(BASE_DIR, 'templates') is for django-rest-framework.