Can't modify django rest framework base.html file - python

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.

Related

Where is django TEMPLATES

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

Django - templatedoesnotexist

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.

django 1.9 and registration/login.html

I'm working on a django 1.9 project.
With Django 1.7.7, login functionnalities was working, but now all the time I have : registration/login.html : Template Does Not Exist
The templates login.html, logout.html are present in 'webgui/template/registration/' and I didn't modified them.
Here some of my settings.py :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'webgui',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'project.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',
],
},
},
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'NCIS.db'),
}
}
STATIC_URL = '/static/'
LOGIN_REDIRECT_URL = '/login/'
LOGOUT_URL = '/logout/'
DIRS = (
join(BASE_DIR, 'webgui/template/registration'),
join(BASE_DIR, 'webgui/template/')
)
And my urls.py :
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth.views import login, logout
import webgui.views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', login, name='login.html'),
url(r'^login/$', login, name='login.html'),
url(r'^logout/$', logout, name='logout.html'),
url(r'^homepage/$', webgui.views.homepage),
url(r'^addproject/$', webgui.views.addproject)
]
What's wrong? I checked the Django docs, but that's the default behaviour.
This question appears first in search engine with searching for
registration/login.html : Template Does Not Exist
So for those coming through search engine, please note that this template doesn't come by default with Django. If you haven't created it, that's why you are getting this error
Here is how you can create a simple one
https://simpleisbetterthancomplex.com/tutorial/2016/06/27/how-to-use-djangos-built-in-login-system.html
Try to put your template dirs paths in DIRS list inside TEMPLATES setting. (Anyway, your template folder name should be templates not template.)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [join(BASE_DIR, 'webgui/template/registration'),join(BASE_DIR, 'webgui/template/')],
'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',
],
},
},
]
After upgrading my django to 1.9.1, same thing happened to me. Apparently, there are updates on templates directory.
Here is how I fixed it.
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',
],
},
},
]
Of course you should have BASE_DIR defined
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
After that,I've deleted the templates folder and created a templates folder for each app. So, inside the each app, just create templates and put the html files inside.
Also in views, connect it to the html file like this.
def index(request):
context_dict = {}
return render(request, 'index.html', context_dict)
This worked for me.
You have set 'APP_DIRS': True,, so Django will search for templates directories inside each app in INSTALLED_APPS, including your webgui app.
The problem is that you have named your directory webgui/template/ instead of webgui/templates/, so the app loader won't find it.
The easiest fix is to rename your directory. If you don't want to do this, you'll have to add the webgui/template directory to your DIRS option.

How do I properly structure Templates and include them in the settings in Django 1.8.6?

So, here is my current structure:
django_project/
home_app/
__init__.py
migrations/
templates/
home_app/
base.html
admin.py
models.py
views.py
project_backend/
__init__.py
settings.py
url.py
wsgi.py
Now in home_app/views.py I have this very simple piece of code:
from django.views.generic import TemplateView
class HomeView(TemplateView):
template_name = "home/base.html"
and in the settings.py I set up my templates like this:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'home_app', 'templates'),
# os.path.join(BASE_DIR, 'app_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',
],
},
},
]
This works fine, and I am quite happy using the app/templates/app/some_temp.html approach, but this only works when I include the OS.path.joins for all the apps and templates, as far as I can see. Is there a way to register this structure in the settings by default and I am just overlooking it?
This is just no very pretty and I feel like it would be bad practice to write that down for every single app.:
os.path.join(BASE_DIR, 'home_app', 'templates')
You have 'APP_DIRS': True, so you shouldn't have to edit DIRS to use templates in the home_app/templates/ directory, as long as home_app is in your installed apps.
If you have a template,
home_app/templates/home_app/base.html
then you should use it with:
template_name = "home_app/base.html"
You currently have,
template_name = "home/base.html"
which isn't consistent.

Django TemplateDoesNotExist Error on Windows machine

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',
],
},
},
]

Categories