I have an error on my Django Project. The templates folder doens't work. I have the project like this:
├── manage.py
├── myProjet
│ ├── __init__.py
│ ├── settings.py
│ ├── templates
│ ├── urls.py
│ ├── wsgi.py
│ └── wsgi.pyc
├── app1
├── templates
So, I want to use the templates forlder. The template filder is on "/myProjet/templates", not on: "/myProjet/myProjet/templates." The settings.py file is this:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [ os.path.join( '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, the error is raised on the signup function. The singup function is:
def SignupPage(request):
if request.method=='POST':
uname=request.POST.get('username')
email=request.POST.get('email')
pass1=request.POST.get('password1')
pass2=request.POST.get('password2')
if pass1!=pass2:
return HttpResponse("Your password and confrom password are not Same!!")
else:
my_user=User.objects.create_user(uname,email,pass1)
my_user.save()
return redirect('login')
return render (request,'signup.html')
The error information is this. I have tried to change the base_path in a lot of ways but I always have this error :
TemplateDoesNotExist at /
signup.html
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 4.1.6
Exception Type: TemplateDoesNotExist
Exception Value:
signup.html
Exception Location: /home/myPC/myProject/my_env/lib/python3.8/site-packages/django/template/loader.py, line 19, in get_template
Raised during: app1.views.SignupPage
Python Executable: /home//myProject/my_env/bin/python
Python Version: 3.8.10
Python Path:
['/home/myPC/myProject/pmTools_V1',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/myPC/myProject/my_env/lib/python3.8/site-packages']
I don't know how to correct the erorr. I have a 3.8 python version and 4.1.6 DJango version.
You can use separate folder for templates but you need to probably fix the DIR in TEMPLATES as follows:
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',
],
},
},
]
try
'DIRS': [ os.path.join(BASE_DIR,'templates')],
or at least give the full path depending where templates should be.
By the way: the error message "TemplateDoesNotExist at /" is missleading as the "/" is not a search path. You always get that message whenever a template is missing that you address like "xyz.html".
TemplateDoesNotExist at /
signup.html
Below the error message that you have posted you should get the info about where the template has been searched for ... something like this:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: C:\ ..... # because of 'DIRS': ...
django.template.loaders.app_directories.Loader: C:\ ..... # because of 'APP_DIRS': True,
django.template.loaders.app_directories.Loader: C:\ ..... # because of 'APP_DIRS': True,
Related
I am trying to render a template in django.
I get this error when i connect to the site:
django.template.loaders.filesystem.Loader: path-to-azerty/templates/base.html (Source does not exist)
Here's my project directory structure:
azerty/
__init__.py
├── settings.py
├── templates
│ └── base.html
├── urls.py
├── views.py
└── wsgi.py
Here's my code:
// ulrs.py
from django.contrib import admin
from django.urls import path
from azerty import views
urlpatterns = [
path('', views.index),
path('admin/', admin.site.urls),
]
// 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',
],
},
},
]
//views.py
from django.shortcuts import render
def welcome(request):
return render(request, 'base.html')`
Your 'DIRS' setting is for a templates directory in your project directory (the one that contains manage.py).
If you want the templates to be on the inner directory (the one that contains settings.py, then you need to change it to:
os.path.join(BASE_DIR, 'azerty', 'templates')],
Another option that is sometimes used is to add azerty to your INSTALLED_APPS. Then the app loader will find the directory, and you can use 'DIRS': [],
I tried to use render to show the HTML edited on PyCharm, but when I entered the address: 127.0.0.1:8000/index/, the following TemplateDoesNotExist exception appeared:
TemplateDoesNotExist at /index/
index.html
Request Method: GET
Request URL: http://127.0.0.1:8000/index/
Django Version: 1.11.1
Exception Type: TemplateDoesNotExist
Exception Value:index.html
Exception Location: D:\python3.6.1\lib\site-packages\django\template\loader.py in get_template, line 25
Python Executable: D:\python3.6.1\python.exe
Python Version: 3.6.1
Python Path:
['C:\Users\Administrator\guest',
'D:\python3.6.1\python36.zip',
'D:\python3.6.1\DLLs',
'D:\python3.6.1\lib',
'D:\python3.6.1',
'D:\python3.6.1\lib\site-packages']
Server time: Fri, 2 Jun 2017 03:30:58 +0000`TemplateDoesNotExist at /index/
settings.py:
ROOT_URLCONF = 'guest.urls'
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',
],
},
},
]
views.py:
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,"index.html")
Screenshot of PyCharm:
I have tried some methods provided on StackOverflow, but they do not work. Is this exception caused by the wrong dirs? How can I deal with such situation?
Look at this:
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',
],
},
},
]
Specifically
'DIRS': [],
You need to tell Django where to look for your templates. If you have a templates folder in your main project root. Then, add
os.path.join(BASE_DIR, 'templates')
This assumes your project has this structure:
root
app1
...
templates/
what we're referencing with os.path.join(BASE_DIR, "templates")
....
static/
what we're referencing with STATIC_ROOT = os.path.join(BASE_DIR, "static")
...
If you are placing templates inside of your apps. There are two things.
Your app inside your project will need this structure for the template folder
app name (folder)
templates (folder)
app name (folder)
(template files) e.g. - "base.html"
You will need to add this line
os.path.join(BASE_DIR, APP NAME, "templates")
Addendum: Undoubtedly you will run into this problem as well... so let's go ahead and cover it.
If you are extending / including templates which are in an app (not in the base template folder) you will need to reference where they are located:
Example: I have a template in "myapp/templates/myapp/template.html" and I want to include it.
Then, I'd do {% include "myapp/template.html" %} or if extending {% extend "myapp/template.html" %}. If they are in the base template folder then you can just reference them directly as "template.html"
I have the same issue. It was mainly caused if you didn't install your app in "settings.py" file
Just do this in your settings.py file and your error will be gone
INSTALLED_APPS = [
'<app name>.apps.<app name>Config',
]
In the place of paste your "app name"
I'm creating a project named "crepes_bretonnes". In this project, I have an application blog. I created a template date.html. Here is the structure of my folders :
crepes_bretonnes/
blog/
__init__.py
admin.py
migrations/
__init__.py
models.py
templates/
blog/
addition.html
date.html
tests.py
views.py
crepes_bretonnes/
__init__.py
settings.py
urls.py
wsgi.py
templates/
db.sqlite3
manage.py
When I try to see the page, I have a message templateDoesNotExist. I have read a lot about it on the web but I have not succeed in resolving my problem. In fact, I don't understand why Django does not search in my template folder of the app blog although I wrote "blog" in INSTALLED_APP in setting.py. Obviously, I have put TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),) in setting.py. I also tried to change the dictionary TEMPLATES. However, if I have understood well, it has no link as here Django should find my template even without this.
I don't have any solution.
Thank you for your help.
PS: If I put date.html in the general template folder and I arrange some lines, it works. However that is not a solution, I would like to respect a good structure.
UPDATE:
Thank you for your answer. Yes it really says INSTALLED_APPS in my setting and APP_DIRS is already True.
Here is my TEMPLATES in setting.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',
],
},
},
]
UPDATE:
The debug message shows that Django search in django.template.loaders.app_directories.Loader: /Users/benjamin/anaconda/lib/python2.7/site-packages/django/contrib/admin/templates/blog/date.html.
However I have not written anything in these folder ... I'm working in Documents. Why does Django search here and not in Documents ?
I solved it by adding forward slash "/" after 'templates':
'DIRS': [BASE_DIR / 'templates/'],
I had the same problem saying template does not exist and I solved it by changing the URL pattern in my views.py where previously I had mentioned just date.html, later I edited with blog/date.html. Hope it might help you.
I assume 'INSTALLED_APP' is a typo and it really says 'INSTALLED_APPS' in your settings. Anyway, you need to set APP_DIRS = True for templates to be found in app/templates
folders:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True, # this line is important
'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'
# ...
]
},
},
]
me having the same issue,
added the app to the INSTALLED_APPS
done, because you (and me) were using the templates not in the main app so it must be registered in the settings (like referring to the urls of the app from the main urls file)
Go to your setting.py and try something like:
os.path.join(BASE_DIR, "templates")
For example:
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',
],
},
},
]
For me, it was using rest_framework without adding it to INSTALLED_APPS.
I have been following the tutorial for Django Tango with Django
I was trying to add a template as instructed on the link.
I am working with Python 2.7, Django 1.8 on a windows 7 machine.
Below is the error that I get:
TemplateDoesNotExist at /rango/
rango/index.html
Request Method: GET
Request URL: http://127.0.0.1:8000/rango/
Django Version: 1.8
Exception Type: TemplateDoesNotExist
Exception Value:rango/index.html
Exception Location: C:\Python27\lib\site-packages\django\template\loader.py in get_template, line 46
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Python27\lib\site-packages\django\contrib\admin\templates\rango\index.html (File does not exist)
C:\Python27\lib\site-packages\django\contrib\auth\templates\rango\index.html (File does not exist)
Below is my file structure:
tango_with_django_project
+-- rango
| +--views.py
| +--other files
+-- tango_with_django_project
| +--templates
| | +--rango
| | | +--index.html
| +--settings.py
| +--other files
+-- db.sqlite3
+-- manage.py`
I have given the template path as below in settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR,'templates')
TEMPLATE_DIRS = (
TEMPLATE_PATH,
)
And have set the view as in views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
context_dict = {'boldmessage': "I am bold font from the context"}
return render(request, 'rango/index.html', context_dict)
Without using the template, it works fine and displays plain text. But when I do the changes required for the template, it does not work and throws the Error.
There are many links on SO with the same issue but none have worked for me.
The one with almost the same Error description and on Windows machine has a vague answer.
I have tried directly specifying the absolute path instead of getting it from os.path and have also tried placing the template folder in different paths.
Any help would be appreciated.
The solution that worked for me was removing the below piece of code from 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',
],
},
},
]
And adding :
TEMPLATE_DIRS = ('C:/Users/vaulstein/tango_with_django_project/templates',)
OR
Changing the line 4th line below :
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['C:/Users/vaulstein/tango_with_django_project/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',
],
},
},
]
You can simply add the application name to INSTALLED_APPS list in settings.py
find the following list in settings.py,
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles'
]
add your application name to that list. In your case,
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tango_with_django_project'
]
In my case modifying INSTALLED_APPS doesn't help, so based on previos answers I just modified my settings.py in the following way:
Add import OS
Add os.getcwd() into DIRS
So it looks like this:
...
from pathlib import Path
import os
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.getcwd()],
'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',
],
},
},
]
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.