What is the correct way to prepend an extra, site-specific templates directory to the template dirs setting in Django?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
<PREPEND SITE-BASED TEMPLATE DIR IN HERE>
str(APPS_DIR.path('templates')),
],
'OPTIONS': {
'debug': DEBUG,
'loaders': [
'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.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 obviously can't use Site.objects.get_current() in there.
I could write my own template loader which uses it but is that "correct"?
Or should I be using an extended version of the settings files or something?
First of all, start by separating the settings. You should have a settings hierarchy that goes like:
base_settings.py
siteA_settings.py
siteB_settings.py
On base_settings.py you should put all the settings that are project-wide.
siteA_settings.py (which is a child) will override the base_settings.
To make it site-specific you have to put on each setting file the SITE_ID setting. Example of siteA_settings.py
# Sites Framework
SITE_ID = 1
SITE_TITLE = 'First Site'
Then load the TEMPLATES settings. They can be different on each setting file. Example:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
... , etc.
Just like TEMPLATES, you can choose to have different settings for each Site (apps, email settings etc)
Then, when you run the server, you should specify which setting (site) you want to run. Example:
python manage.py runserver --settings=myproject.siteA_settings
where myproject is the app where your settings are located.
Do not forget that you have to put the --settings argument everytime you run a command (including makemigrations, migrate, etc etc)
Usually, on my projects, I create a core app, a settings directory and inside it, I put base_settings.py, dev.py, local.py, production.py etc. When running the Django Development Server, I then run the command:
python manage.py runserver --settings=core.settings.local
Related
I'm using django-invitations to manage user invitation.
I can't find where I should put the template files email_invite_message.txt and email_invite_subject.txt
Here they talk about
Override email_invite_message.txt in django-invitations/invitations/templates/invitations/email/.
You can do this by creating the file in the same directory path in your project.
or
Yeah, if this isn't clear, you can create an .html file in your project at {projectroot}/{app}/templates/invitations/email/email_invite_message.html and it will override the default template.
But the first one didn't work for me and I can't figure out what {app} should be.
You can override any third-party application templates by creating replacement files in your project’s templates directory as indicated in the docs here.
In your settings.py file edit the TEMPLATES var, specifically the 'DIRS' key
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR + '/core/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',
],
},
},
]
let's suppose you have an app named core, edit the settings as in the previous example and put the new files in this way (for this particular app):
core/
/templates
/invitations
/email
email_invite_message.html
email_invite_subject.html
__init__.py
admin.py
apps.py
models.py
tests.py
How to define a directory for storing all templates of my app? I am having a hard time finding the explanation how to set templates directory in Django settings.py file.
To set your path to templates folder you need to set your DIRS key with the value in which you are giving your path to templates as shown in the example below where at first i am setting a variable TEMPLATES_DIR where BASE_DIR give me the path to the folder where my manage.py is being kept and i concatinate projects directory alongwith templates directory because in my case i kept it inside project directory.
For example :-
TEMPLATES_DIR = os.path.join(BASE_DIR, 'project','templates')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'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',
],
},
},
]
So, Here's the explaination What i did is set the TEMPLATES_DIR variable with reference to base directory that will take the path to folder where manage.py of your django project is being kept now i am concatinating the projects and templates directory just to navigate the exact folder structure that you kept for your templates in your project.
I hope you found this helpful. if you have any more doubts i'll be happy to answer those also.
Happy Coding. :-)
The way to set your templates directory in Django is by setting the value of the 'DIRS' key in TEMPLATES inside the settings.py like so (assuming it is present just inside your base directory (BASE_DIR):
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',
],
},
},
]
In Django 3, the templates directory can be added to 'DIRS' like this as well:
'DIRS': [str(BASE_DIR.joinpath('templates'))]
I set an address for the DIRS in TEMPLATES setting in my project's settings.py file and also i change APP_DIRS to False.
now, all of the templates will running but when i go to localhost:8000/admin, it generates an error that said "Template Does Note Exists".
TemplateDoesNotExist at /admin/
admin/index.html
i dont want to APP_DIRS be True.
how can i solve this problem?
this is the settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': False,
'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',
],
},
},
]
If you dont want APPS_DIR to be TRUE, copy templates of admin app to your template directory. When you set APP_DIR to True django will check for a directory named templates in each apps and if found load specified templates from that directory. Else django will check for a directory with same name as of app in the global template directory if found render specified template.
In you case APP_DIR is set to False and there is no directory named as admin in global template folder. So copy template directory from django/contrib/admin/ of your virtual environment and place it in your global template directory. I suppose your global template directory is in the project directory itself.
os.path.join(BASE_DIR, 'templates'),
HTH :)
I have a context processor which makes constants available in templates, including base template.
On my vagrant development server, using runserver, these work fine. The production server uses apache and WSGI.
I'm using Django 1.9.4 on both development and production.
This is the context_processors.py
from .constants import *
def template_constants(request):
return {'collection_name': COLLECTION_NAME,
'website_name': WEBSITE_NAME,}
This is from settings.py
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',
'django.template.context_processors.request',
'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',
'myapp.photos.context_processors.template_constants',
],
},
},
]
Constants are just in a file constants.py in my app, which I import into files which use them. The file has lines such as:
WEBSITE_NAME = "website name"
My views use render which should be including RequestContext
return render(request, 'page.html', template_variables)
I get no error messages, the variables simply don't have a value in the templates on production, so nothing is shown.
Apart from deployment, the code on production and dev is exactly the same.
Things I've tried:
- Adding string value directly into context_processors.py file (eliminating it being a potential issue importing variables from constants.py)
- Going back to Django 1.8 and 1.7 to check if it's something related to a specific version
None of these changes made any difference.
I'm going through the very basic tutorial here and I am using my own base_site.html template instead of the default one, which has a different header text.
I copied the default template into my template folder, modified it, then linked the new template in settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
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',
],
},
},
]
No dice.
When I went a much simpler route and instead simply linked it with the single line below, it worked easily:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates/')]
Which is great! But now I want to know why the original code doesn't work.
If it helps, my folder structure is:
- Scripts
- mysite
- mysite
- polls
- templates
- admin
base_site.html
The TEMPLATES setting will be introduced in the django 1.8. It is unavailable in the current django 1.7.
So read the tutorial for the actual version of django you using.