How to build a path in my templates file for Django - python

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

Related

how can i use path Django for locating and loading templates? Django 3.2

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)

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 Across All Apps

I am creating a rather large django project which will require two things. 1) I need a few template files to be accessible across all apps. 2) I need a model to be accessible across all apps. How do I go about doing that?
As far as the templates are concerned, it seems adding it to the TEMPLATES directive doesn't work.
As far as the models are concerned, can I have a models.py in the project/project folder or something like that to be accessible by all apps?
Django 1.10 and Python 3.5
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
[...]
Was able to work it out with this work around.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR + '/templates'],
'APP_DIRS': True,
'OPTIONS': {
[...]

Trouble overriding django/grappelli base.html template

According to the Django tutorial I make a templates directory under BASE_DIR, and add both an admin and an Grappelli subfolder, later copying base.html from both and putting each in its respective directory. Then I make some changes, add some CSS and JS... Reload the test server but no change is reflected at my admin interface, even the CSS/JS I add is not there!
my TEMPLATES in 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',
'django.core.context_processors.request',
],
},
},
]
i must be doing something utterly wrong, but i have no idea what and the documentation isn't helping.
ANSWER: The problem was the 'DIRS': [] only working in django 1.8+, for Django 1.7- we need to use the following:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
my settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_DIR = os.path.dirname(os.path.dirname(__file__) + '/../')
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.request',
'apps.projects.context_processors.status',
)
SITE_ID = 1
ROOT_URLCONF = 'apps.urls'
WSGI_APPLICATION = 'apps.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
STATIC_URL = '/static/'
STATIC_ROOT = PROJECT_DIR + '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = PROJECT_DIR + '/media/'
TEMPLATE_DIRS = (
PROJECT_DIR + '/templates/',
)

What is the path for TEMPLATE_DIRS in django settings.py when using virtualenv

I am using virtualenv and I want to know what the TEMPLATE_DIRS in settings.py should be, for example if I make a templates folder in the root of my project folder.
You need to specify the absolute path to your template folder. Always use forward slashes, even on Windows.
For example, if your project folder is "/home/djangouser/projects/myproject" (Linux) or 'C:\projects\myproject\' (Windows), your TEMPLATE_DIRS looks like this:
# for Linux
TEMPLATE_DIRS = (
'/home/djangouser/projects/myproject/templates/',
)
# or for Windows; use forward slashes!
TEMPLATE_DIRS = (
'C:/projects/myproject/templates/',
)
Alternatively you can use the specified PROJECT_ROOT variable and generate the absolute path by joining it with the relative path to your template folder. This has the advantage that you only need to change your PROJECT_ROOT, if you copy the project to a different location. You need to import the os module to make it work:
# add at the beginning of settings.py
import os
# ...
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, 'templates/'),
)
If you're working with a newer version of Django you may have to add it to the DIR list that is inside settings.py under TEMPLATES.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['[project name]/templates'], # Replace with your project name
'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',
],
},
},
]
the PROJECT_DIR has not been defined... the PROJECT_DIR is not a variable. its a directory/ a path to where the folder "templates" is located. This should help
import os
PROJECT_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = os.path.join(PROJECT_DIR, 'templates')
TEMPLATE_DIRS deprecated
This setting is deprecated since Django version 1.8.
deprecated
""" settings.py """
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates/'),
)
correct
""" settings.py """
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [ os.path.join(BASE_DIR, 'templates') ],
'APP_DIRS': True,
...
},
]
If you are using Django 1.9, it is recommended to use BASE_DIR instead of PROJECT_DIR.
# add at the beginning of settings.py
import os
# ...
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates/'),
)
Adding this in web/settings.py solved everything for me. Hope it can help you too.
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
from os.path import join
TEMPLATE_DIRS = (
join(BASE_DIR, 'templates'),
)

Categories