There are two sites with different domains (first_example.com, second_example.com). They have a common database and common logic.
The task is what needs to be done so that for each of the sites its own template is loaded.
For example, there will be a file structure
__landing
____templates
______landing
________site_1
__________ index.html
________site_2
__________ index.html
It is necessary that when opening the first domain, templates from site_1 are loaded.
And when opening the second domain, templates from site_2 were loaded.
I think I need to write somehow template_loader, but I don’t understand yet how to do it.
Instead of nesting the "per-site" templates within the template directory structure, use two distincts root template directories, ie instead of
/templates
/app-one
site1/
index.html
site2/
index.html
you want:
/site1-templates
/app-one
index.html
/site2-templates
/app-one
index.html
Then in each setting file (you have a distinct settings for each site do you ?), just specify the proper path for TEMPLATES.DIRS
# site1 settings
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(PROJECT_ROOT, 'site1-templates'),
],
'OPTIONS': {
# etc
}
}]
NB: if you need to keep some common templates, you can have them in a third directory (ie basetemplates) and add this after the site-specific path in TEMPLATES.DIRS.
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(PROJECT_ROOT, 'site1-templates'),
os.path.join(PROJECT_ROOT, 'base-templates'),
],
'OPTIONS': {
# etc
}
}]
Related
I'm a beginner in Django and don't understand how the templates "dirs" in setup.py works
In my project directory, I have 2 apps. I want to reach them via a navigation bar.
In the Navigation bar, I have "Home" "Manipulation" "Visualization"
I can reach Home and Manipulation page, but when I try to reach the Visualization page, the browser shows me the Manipulation page again.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'manipulation/templates/manipulation'),
os.path.join(BASE_DIR, 'visualization/templates/visualization'),
],
'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',
],
},
},
]
Somebody can help me please?
Thank you very much
In Dirs : [], when I swap the order and put the "Visualization" path before the "Manipulation" path, I get the Visualization page instead of the Manipulation page
if you have
'APP_DIRS' = True,
django will automatically look for templates in all app_name/templates/ folders
point to templates always as "app_name/template_name.html"
(which will then point to "app_name/templates/app_name/template_name.html")
store the templates as app_name/templates/app_name/templates_name.html"
That way you can have an individual e.g. home.html in each app without conflicts.
in 'DIRS' you should only add directories that you want to add outside the apps systematic. Currently you are overwriting the normal search in you your app folders and together with a missing "app_name/" in your "/app_name/template_name.html" you get the behavior that you describe
Let's say I have the following directory structure
|-data
|-include_this.html
|-app
|- templates
|- app
|- page.html
In page.html, I'd like to include the include_this.html file contained in the data folder.
I have tried the following:
{% include '../data/include_this.html' %}
{% include '../../data/include_this.html' %}
{% include './../data/include_this.html' %}
But none of them seem to work, and the second one throws the error: The relative path points outside the file hierarchy that template is in.
Re: #Edoardo Facchinelli suggestions, I've modified settings.py in the following way:
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'data')],
'APP_DIRS': True,
}]
But the include_this.html is still not being recognized.
Running
python manage.py shell
from django.conf import settings
print(settings.TEMPLATES[0]['DIRS'])
Returns:
['_templates']
So it's clearly not adding the data directory specified. What's the correct way to do this?
You need to make sure that the path is known to Django, either as an app, or in this case a template dir. One way to do this is explained here that I think is done well, from that page here's a snippet for the settings.py file, enabling the templates dir.
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
...
With other configurations you may need instead the TEMPLATE_DIRS variable, but that will depend on your project, mine use the method in the snippet above.
Once set up, Django will know what data means and you should be able to reference it like so:
{% include 'data/papercups.html' %}
I tried overriding a django-recaptcha template without any luck. What am I doing wrong?
I am aware of Override templates of external app in Django, but it is outdated. Thanks!
django-recaptcha file structure
Lib/
--site-packages/
----captcha/
------templates/
--------captcha/
----------includes/
------------js_v2_checkbox.html
my project file structure
project/
----templates/
--------captcha/
------------includes/
----------------js_v2_checkbox.html
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',
],
},
},
]
You have two options:
(1) In your settings, rearrange INSTALLED_APPS as follows:
INSTALLED_APPS = [
...
'project',
...
'captcha',
...
]
since the template loader will look in the app’s templates directory following the order specified by INSTALLED_APPS, you're template will be found first.
or
(2) List project's templates folder in TEMPLATES[0]['DIRS'] as follows:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
...
},
]
Since, DIRS is searched before APP_DIRS, you're template will be found first.
References:
https://docs.djangoproject.com/en/3.0/howto/overriding-templates/
Another possible solution
I notice now that captcha/includes/js_v2_checkbox.html is included by captcha/widget_v2_checkbox.html.
I'm not sure about what happens exactly when widget_v2_checkbox.html is loaded from captcha module ... so I would duplicate the "including" widget_v2_checkbox.html as well into your project's templates folder.
You might also decide to copy the full "templates/captcha" folder contents into you project, for consistency.
Just keep an eye on possibile future changes of those templates when upgrading the captcha module.
Can I see the order of your INSTALLED_APPS? It could be that you are rendering django-recaptcha captcha templates before you render your own templates. To fix this you could move the the external app captcha last in the INSTALLED_APPS list.
I am making a django app and when I was reading about template loaders I saw this:
You can enable this loader simply by setting APP_DIRS to True:
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
}]
what is this doing ? and what are the uses of DIRS?
DIR is stand for direction which is allow you to read directory files and use it in your django project.
for example look at this line of code :
'DIRS': [os.path.join(BASE_DIR, 'templates')],
it allow you too read all files in templates folder in your directory and render your templates like yourHTMLfile.html or others and also it could have multiple value.
I have seen this question answered but I can't get it to work on my site. I have installed a site using Django 1.8 as I wanted to use the django-admin-bootstrapped plugin. I am also using the template provided by Heroku.
What I would like to do is override at the very least the base_site.html to change the site title and page title. However, I have tried several locations for this file including the following:
PROJECT ROOT
|--templates
|--admin
|--base_site.html
PROJECT ROOT
|--templates
|--admin
|--<appname>
|--base_site.html
PROJECT ROOT
|--<appname>
|--templates
|--admin
|--base_site.html
PROJECT ROOT
|--<appname>
|--templates
|--admin
|--<appname>
|--base_site.html
None of which work. I have the following in my 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',
],
'debug': DEBUG,
},
},
]
I am just wondering if I am doing something fundamentally wrong or if I just haven't found the magic location for the templates folder (or order of folders etc).
On a related note, is there anyway of debugging where the templates are being pulled from or is it just guesswork based on where they are supposed to be coming from?
Not every template in contrib/admin/templates/admin may be overridden per app or per model. The following can:
app_index.html
change_form.html
change_list.html
delete_confirmation.html
object_history.html
enter link description here
Override change_list.html to change the site title and page title