Overriding Admin Templates in django 1.11 - django

I don't get django 1.11 to accept a new base_site.html:
Following docs and some posts i copied the file to:
BASE_DIR/templates/base_site.html and
BASE_DIR/templates/admin/base_site.html
in settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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',
],
'loaders': (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
),
},
},
]
I tried that with the file in templates and in templates/admin:
>>> print ( os.path.isfile((os.path.join(settings.BASE_DIR, 'templates', 'admin','base_site.html'))) )
True
>>> print ( os.path.isfile((os.path.join(settings.BASE_DIR, 'templates', 'base_site.html'))) )
True

If you want to override admin files they must exist in admin folder inside templates folder like below
your_project
your_project
|-- your_project/
|-- myapp/
|-- templates/
|-- admin/
|-- base_site.html
here file name base_site.html must be same as the file name in original admin folder.
base_site.html
{% extends "admin/base.html" %}
# you can write changes here

Just had this problem and literally fixed it by installing django-apptemplates and adding it as a loader into settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [PROJECT_DIR],
'APP_DIRS': False,
'OPTIONS': {
'loaders': [
# ADD LOADER HERE
'apptemplates.Loader',
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
**APP_DIRS will have to be set to False in order for this to work

To override the base_site.html of Django admin, add the custom base_site.html such that it's path is BASE_DIR/templates/admin/base_site.html. This works in Django 1.11 as well.

Related

How can I fix django error? "template does not exist"

I started Django few days ago and I am stuck now.
I got error like this.
django.template.exceptions.TemplateDoesNotExist: home.html
but I don't know how to fix it.
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import ToDoList, Item
def index(response, id):
ls = ToDoList.objects.get(id=id)
return render(response, "main/base.html", {"name":ls.name})
def home(response):
return render(response, "main/home.html", {'name':'test'})
base.html
<html> <head> <title>Website</title> </head> <body> <p>{{name}}</p> </body> </html>
home.html
{% extends 'main/base.html' %}
Both html files are in file named templates
I thought template might have something to do with it and checked it.
setting.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',
],
},
},
]
Does someone help me out?
Just add your root template folder name to the DIRS value.
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",
],
},
},
]
You have to have both files in folder: <project_name>/<app_name>/main/, but you probably put it somewhere else.
Additionally, it's good practice to add this value to TEMPLATES["DIRS"]:
"DIRS": [os.path.join(PROJECT_DIR, "templates")],
and then create folder "templates" inside every app you have and store templates inside (move there your whole folder "main" with proper templates)
do this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR,"templates",
os.path.join(BASE_DIR,"appname","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',
],
},
},
]

Pointing Template Engine to Project Templates Directory

I'd like to store templates that all app templates will extend from.
Current layout of project
mysite/
-(virtualenv folders...)
- wesdrew/ # project root
- static/
- templates/
- base.html # simple templates to test loading
- wesdrew/ # 'home page' app
In wesdrew/settings.py:
PROJECT_TEMPLATES='../templates'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
Is there a way to override the template engine looking for '/templates/wesdrew'

Django 1.11 use template from an app as admin template

as the title said i am having a problem using a template in an admin view
here is my worktree
project
|-- project/
|-- myapp/
|-- templates/
|-- admin/
|-- file.html
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'myapp/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',
],
},
},
]
ModelAdmin.py
class ModelAdmin(admin.ModelAdmin):
actions = ['export']
def export(self,request, queryset):
paragraphs = ['first paragraph', 'second paragraph', 'third paragraph']
pdfkit.from_file('file.html', 'out.pdf', paragraphs)
admin.site.register(Model, ModelAdmin)
but i am getting "No such file: file.html" error
pdfkit does not know about your TEMPLATES setting. You need to provide the path relative to your project directory.
pdfkit.from_file('myapp/templates/admin/file.html', 'out.pdf', paragraphs)
However this will treat the file.html as an html file. If it is a Django template that you want to render, you could use render_to_string and from_string.
from django.template.loader import render_to_string
html = render_to_string(request, 'admin/file.html', {...})
pdfkit.from_string(html, 'out.pdf')

templets not found error in django

I am little bit confused about django templates, I have attached screen shot of workspace, can some one please tell how can I use addprofile.html template in my views.py in buddy app.
In my settings/base.py I have mentioned templates in following way:
# Build paths inside the project like this: join(BASE_DIR, "directory")
BASE_DIR = dirname(dirname(dirname(__file__)))
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATICFILES_DIRS = [join(BASE_DIR, 'static')]
MEDIA_ROOT = join(BASE_DIR, 'media')
MEDIA_URL = "/media/"
# Use Django templates using the new Django 1.8 TEMPLATES settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
join(BASE_DIR, 'templates'),
# insert more TEMPLATE_DIRS here
join(PROJECT_PATH, 'templates/')
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'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',
],
},
},
]
I am new to Django and python and this is my first app in django
create following structure:
buddy (this is your existing budy app folder)/templates (this is existing folder)/buddy (this is new folder) and place all your templates: addprofile.html, edit_profile.html, show_profile.html, success.html withing this path.
edit your settings 'DIRS' so it looks as follows:
'DIRS': [
join(BASE_DIR, 'templates'),
join(PROJECT_PATH, 'templates/'),
join(BASE_DIR, 'buddy/templates')

Django 1.6 Admin Page Overriding Not Working

We're trying to override the admin page for Django 1.6, but it continues to get it from django/contrib/templates/...:
settings.py:
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, 'templates')],
],
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'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',
],
'loaders': [
# insert your TEMPLATE_LOADERS here
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
]
},
},
]
]
and the files structure:
project
project
templates
app_name
admin
file_to_override <--it varies here from being inside the app_name or inside template itself
I'm not sure why the directories arent working
I don't know why you're using Django 1.6, but the TEMPLATES dict syntax you quote is only for Django 1.8+. In previous versions, you need to specify all the options individually.
Also note you have wrongly surrounded the DIRS value with two list brackets.
TEMPLATE_DIRS = [
os.path.join(BASE_DIR, 'templates'),
]
TEMPLATE_CONTEXT_PROCESSORS = [
'django.contrib.auth.context_processors.auth',
'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',
]
TEMPLATE_LOADERS = [
# insert your TEMPLATE_LOADERS here
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
]