I have installed jinja2 and after that 'DIRS' stopped working(I have to include them by hand).
Changing 'APP_DIRS' doesn`t help
templates look like that:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'APP_DIRS': False,
'DIRS': ['main/templates', 'shop/templates'],
'OPTIONS': {
'environment': 'django_test.create_jinjia_env.environment',
'autoescape': True,
'auto_reload': DEBUG,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
If don`t include templates into DIRS it throws the same error
Didn`t find the questions like that. Thanks in advance!
The Django admin app does not come with Jinja templates. If you wish to use Jinja and the admin app, you need to include both engines in your TEMPLATES setting:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True, # This allows Django to find the templates in the admin app
'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',
],
},
},
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
# The rest of your Jinja2 settings.
},
Secondly, when APP_DIRS is True, the Jinja2 backend looks for templates in a jinja2 subdirectory. That means you should put your templates in main/jinja2 and shop/jinja2 instead of main/templates and shop/templates.
Related
My project structure is roughly as follows:
dinnerproject/
dinnerproject/
settings.py
dinners/
templates/dinners/
main.html
templates/
base.html
manage.py
In settings.py I've got TEMPLATE configured like so:
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',
],
},
},
]
And dinners app is added to INSTALLED_APPS.
I use a TemplateView with template_name = "main.html", which extends base.html.
But when I try to open the page that's supposed to return main.html, I keep getting a TemplateDoesNotExist error saying:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: C:\Users\User\dinnerproject\dinnerproject\templates\main.html (Source does not exist)
(...)
django.template.loaders.app_directories.Loader: C:\Users\User\dinnerproject\dinnerproject\dinners\templates\main.html (Source does not exist)
For some reason, my project name is duplicated in the paths, so django cannot find the right directories. What am I doing wrong?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'dinnerproject','templates'),
os.path.join(BASE_DIR,'dinners','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',
],
},
},
]
urls.py:
app_name="your app name"
This will solve your error
I have deployed my project on pythonanywhere and there i am getting an error that template does not exist
It's working properly on local host
File directory
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
Can someone tell what should i add my template DIRS
Try to put this into DIRS
'DIRS': [os.path.join(BASE_DIR, 'Templates')],
after that you can get your html files like 'Main/test.html'
And yeah it's better to put names in lowercase
I deployed my project on Heroku. But it can't find my templates(Source doesn't exist):
My project architecture:
settings.py:
from pathlib import Path
import sys
import os
BASE_DIR = Path(__file__).resolve().parent.parent
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',
],
},
},
]
UPDATE: All templates from the Account app load with this extending
The problem is solved. There was extra whitespace, like {% extends 'base/base.html ' %}
Erorr
I wanna know where django search for index.html
Setting.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['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',
],
},
},
]
views.py
def index(request):
return render(request, 'index.html', {})
Django will look in the templates directory, that's if you created a folder called templates and if you have a file named index.html in the templates folder. For some reason I feel like the reason why you are asking this question is because you can not get your template to render when you run the server, am I correct? Please get back to me and I can do my best to fill you in so you can get your project working.
You can easily solve this problem: if the name of your project directory, where manage.py is in, is "RalphPortfolio", you need to make the following correction in settings.py:
'DIRS': ['RalphPortfolio/templates'],
add the full path to the template dirs
for example if your path is /home/some_url/your_project/templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/home/some_url/your_project_name/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',
],
},
},
]
it's because the path is not clear well and it will not find it.
I've upgraded Django from version 1.8 to version 1.10 and I am now getting the below error:
No module named context_processors
This is in relation to this code in settings.py. If I comment out the lines that start with 'django.core it runs fine but I obviously will be losing functionality:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
TEMPLATE_PATH,
],
'APP_DIRS': True,
'OPTIONS': {
'debug': DEBUG,
'context_processors': [
...
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.tz',
'django.contrib.messages.context_processors.messages',
...
],
},
},
]
How can I fix this?
Note: This, this and this are all similar but nothing Google returns addresses this issue.
After a lot of digging, I found a fix. Hidden in the documentation is the solution to the issue:
django.core.context_processors
Built-in template context processors have been moved to django.template.context_processors.
Hence, in order to fix the issue, you need to replace django.core with django.template. The code would then look like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
TEMPLATE_PATH,
],
'APP_DIRS': True,
'OPTIONS': {
'debug': DEBUG,
'context_processors': [
...
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
...
],
},
},
]