Standard for application-specific template directories in Django?
I guess this is a question related to best practises in Django development.
I'm trying to build a web service with a main page (base.html
) that contains multiple apps. I would like to make the apps self-contained, so I've made a templates
directory in each app, and would also like to take advantage of the template inheritance feature of Django to make this whole thing as fluid as possible.
Now my concern is, where should I put the base.html
in my project, so that the system knew where to find it?
Also, what changes should I make in the settings.py
file in order for the system to be able to connect the templates? Is there a standard or a known method that takes minimal effort for this sort of arrangement?
django django-templates django-settings
add a comment |
I guess this is a question related to best practises in Django development.
I'm trying to build a web service with a main page (base.html
) that contains multiple apps. I would like to make the apps self-contained, so I've made a templates
directory in each app, and would also like to take advantage of the template inheritance feature of Django to make this whole thing as fluid as possible.
Now my concern is, where should I put the base.html
in my project, so that the system knew where to find it?
Also, what changes should I make in the settings.py
file in order for the system to be able to connect the templates? Is there a standard or a known method that takes minimal effort for this sort of arrangement?
django django-templates django-settings
add a comment |
I guess this is a question related to best practises in Django development.
I'm trying to build a web service with a main page (base.html
) that contains multiple apps. I would like to make the apps self-contained, so I've made a templates
directory in each app, and would also like to take advantage of the template inheritance feature of Django to make this whole thing as fluid as possible.
Now my concern is, where should I put the base.html
in my project, so that the system knew where to find it?
Also, what changes should I make in the settings.py
file in order for the system to be able to connect the templates? Is there a standard or a known method that takes minimal effort for this sort of arrangement?
django django-templates django-settings
I guess this is a question related to best practises in Django development.
I'm trying to build a web service with a main page (base.html
) that contains multiple apps. I would like to make the apps self-contained, so I've made a templates
directory in each app, and would also like to take advantage of the template inheritance feature of Django to make this whole thing as fluid as possible.
Now my concern is, where should I put the base.html
in my project, so that the system knew where to find it?
Also, what changes should I make in the settings.py
file in order for the system to be able to connect the templates? Is there a standard or a known method that takes minimal effort for this sort of arrangement?
django django-templates django-settings
django django-templates django-settings
asked Nov 21 '18 at 14:29
TheSodesa
17010
17010
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
One common design pattern that I have both seen and used is to have a centralized "app" as a part of your project that contains all the shared "stuff" you care to use in other applications. So you might have the following directory structure:
base/
static/
css/
common.css
js/
common.js
templates/
base.html
myapp1/
urls.py
views.py
templates/
...
myapp2/
urls.py
views.py
templates/
...
myproject/
settings.py
urls.py
Now you just include the "base" application just like any other, and you put shared stuff inside it. Other applications can refer to templates that live there, and can include any common libraries that you may want to share.
In settings.py:
INSTALLED_APPS = ['base', 'myapp1', 'myapp2']
Would or should thisbase/
be the same folder as the one containing thesettings.py
file?
– TheSodesa
Nov 21 '18 at 14:49
When I've used this pattern, I have had my settings.py in another folder (typically a "project" level folder). So, imagine another folder at the same level asmyapp1
ormyapp2
that has the same name as the project itself. Inside there is the settings.py file, as well as the project-level URLs (urls.py), the latter of which could include application-specific URLs.
– Jonah Bishop
Nov 21 '18 at 14:52
I will update my answer to show this detail.
– Jonah Bishop
Nov 21 '18 at 14:54
add a comment |
There's no need for a central app for this. The TEMPLATES setting also includes an option for DIRS, which is a list of directories that will always be searched. So you can set this to an appropriate directory - eg os.path.join(BASE_DIR, 'templates')
and put your non-app-specific templates such as base.html there.
add a comment |
as @danialroseman said, you just need to update the DIRS
in TEMPLATES
variable in settings.py
::
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
### ADD YOUR DIRECTORY HERE LIKE SO:
'DIRS': [ os.path.join(BASE_DIR, 'templates')],
...
now create a directory templates
in the project's root folder and put the base.html
file in it.
This is the correct way
– simple_human
Nov 21 '18 at 16:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414298%2fstandard-for-application-specific-template-directories-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
One common design pattern that I have both seen and used is to have a centralized "app" as a part of your project that contains all the shared "stuff" you care to use in other applications. So you might have the following directory structure:
base/
static/
css/
common.css
js/
common.js
templates/
base.html
myapp1/
urls.py
views.py
templates/
...
myapp2/
urls.py
views.py
templates/
...
myproject/
settings.py
urls.py
Now you just include the "base" application just like any other, and you put shared stuff inside it. Other applications can refer to templates that live there, and can include any common libraries that you may want to share.
In settings.py:
INSTALLED_APPS = ['base', 'myapp1', 'myapp2']
Would or should thisbase/
be the same folder as the one containing thesettings.py
file?
– TheSodesa
Nov 21 '18 at 14:49
When I've used this pattern, I have had my settings.py in another folder (typically a "project" level folder). So, imagine another folder at the same level asmyapp1
ormyapp2
that has the same name as the project itself. Inside there is the settings.py file, as well as the project-level URLs (urls.py), the latter of which could include application-specific URLs.
– Jonah Bishop
Nov 21 '18 at 14:52
I will update my answer to show this detail.
– Jonah Bishop
Nov 21 '18 at 14:54
add a comment |
One common design pattern that I have both seen and used is to have a centralized "app" as a part of your project that contains all the shared "stuff" you care to use in other applications. So you might have the following directory structure:
base/
static/
css/
common.css
js/
common.js
templates/
base.html
myapp1/
urls.py
views.py
templates/
...
myapp2/
urls.py
views.py
templates/
...
myproject/
settings.py
urls.py
Now you just include the "base" application just like any other, and you put shared stuff inside it. Other applications can refer to templates that live there, and can include any common libraries that you may want to share.
In settings.py:
INSTALLED_APPS = ['base', 'myapp1', 'myapp2']
Would or should thisbase/
be the same folder as the one containing thesettings.py
file?
– TheSodesa
Nov 21 '18 at 14:49
When I've used this pattern, I have had my settings.py in another folder (typically a "project" level folder). So, imagine another folder at the same level asmyapp1
ormyapp2
that has the same name as the project itself. Inside there is the settings.py file, as well as the project-level URLs (urls.py), the latter of which could include application-specific URLs.
– Jonah Bishop
Nov 21 '18 at 14:52
I will update my answer to show this detail.
– Jonah Bishop
Nov 21 '18 at 14:54
add a comment |
One common design pattern that I have both seen and used is to have a centralized "app" as a part of your project that contains all the shared "stuff" you care to use in other applications. So you might have the following directory structure:
base/
static/
css/
common.css
js/
common.js
templates/
base.html
myapp1/
urls.py
views.py
templates/
...
myapp2/
urls.py
views.py
templates/
...
myproject/
settings.py
urls.py
Now you just include the "base" application just like any other, and you put shared stuff inside it. Other applications can refer to templates that live there, and can include any common libraries that you may want to share.
In settings.py:
INSTALLED_APPS = ['base', 'myapp1', 'myapp2']
One common design pattern that I have both seen and used is to have a centralized "app" as a part of your project that contains all the shared "stuff" you care to use in other applications. So you might have the following directory structure:
base/
static/
css/
common.css
js/
common.js
templates/
base.html
myapp1/
urls.py
views.py
templates/
...
myapp2/
urls.py
views.py
templates/
...
myproject/
settings.py
urls.py
Now you just include the "base" application just like any other, and you put shared stuff inside it. Other applications can refer to templates that live there, and can include any common libraries that you may want to share.
In settings.py:
INSTALLED_APPS = ['base', 'myapp1', 'myapp2']
edited Nov 21 '18 at 14:55
answered Nov 21 '18 at 14:41
Jonah Bishop
8,54733057
8,54733057
Would or should thisbase/
be the same folder as the one containing thesettings.py
file?
– TheSodesa
Nov 21 '18 at 14:49
When I've used this pattern, I have had my settings.py in another folder (typically a "project" level folder). So, imagine another folder at the same level asmyapp1
ormyapp2
that has the same name as the project itself. Inside there is the settings.py file, as well as the project-level URLs (urls.py), the latter of which could include application-specific URLs.
– Jonah Bishop
Nov 21 '18 at 14:52
I will update my answer to show this detail.
– Jonah Bishop
Nov 21 '18 at 14:54
add a comment |
Would or should thisbase/
be the same folder as the one containing thesettings.py
file?
– TheSodesa
Nov 21 '18 at 14:49
When I've used this pattern, I have had my settings.py in another folder (typically a "project" level folder). So, imagine another folder at the same level asmyapp1
ormyapp2
that has the same name as the project itself. Inside there is the settings.py file, as well as the project-level URLs (urls.py), the latter of which could include application-specific URLs.
– Jonah Bishop
Nov 21 '18 at 14:52
I will update my answer to show this detail.
– Jonah Bishop
Nov 21 '18 at 14:54
Would or should this
base/
be the same folder as the one containing the settings.py
file?– TheSodesa
Nov 21 '18 at 14:49
Would or should this
base/
be the same folder as the one containing the settings.py
file?– TheSodesa
Nov 21 '18 at 14:49
When I've used this pattern, I have had my settings.py in another folder (typically a "project" level folder). So, imagine another folder at the same level as
myapp1
or myapp2
that has the same name as the project itself. Inside there is the settings.py file, as well as the project-level URLs (urls.py), the latter of which could include application-specific URLs.– Jonah Bishop
Nov 21 '18 at 14:52
When I've used this pattern, I have had my settings.py in another folder (typically a "project" level folder). So, imagine another folder at the same level as
myapp1
or myapp2
that has the same name as the project itself. Inside there is the settings.py file, as well as the project-level URLs (urls.py), the latter of which could include application-specific URLs.– Jonah Bishop
Nov 21 '18 at 14:52
I will update my answer to show this detail.
– Jonah Bishop
Nov 21 '18 at 14:54
I will update my answer to show this detail.
– Jonah Bishop
Nov 21 '18 at 14:54
add a comment |
There's no need for a central app for this. The TEMPLATES setting also includes an option for DIRS, which is a list of directories that will always be searched. So you can set this to an appropriate directory - eg os.path.join(BASE_DIR, 'templates')
and put your non-app-specific templates such as base.html there.
add a comment |
There's no need for a central app for this. The TEMPLATES setting also includes an option for DIRS, which is a list of directories that will always be searched. So you can set this to an appropriate directory - eg os.path.join(BASE_DIR, 'templates')
and put your non-app-specific templates such as base.html there.
add a comment |
There's no need for a central app for this. The TEMPLATES setting also includes an option for DIRS, which is a list of directories that will always be searched. So you can set this to an appropriate directory - eg os.path.join(BASE_DIR, 'templates')
and put your non-app-specific templates such as base.html there.
There's no need for a central app for this. The TEMPLATES setting also includes an option for DIRS, which is a list of directories that will always be searched. So you can set this to an appropriate directory - eg os.path.join(BASE_DIR, 'templates')
and put your non-app-specific templates such as base.html there.
answered Nov 21 '18 at 14:54
Daniel Roseman
444k41574629
444k41574629
add a comment |
add a comment |
as @danialroseman said, you just need to update the DIRS
in TEMPLATES
variable in settings.py
::
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
### ADD YOUR DIRECTORY HERE LIKE SO:
'DIRS': [ os.path.join(BASE_DIR, 'templates')],
...
now create a directory templates
in the project's root folder and put the base.html
file in it.
This is the correct way
– simple_human
Nov 21 '18 at 16:22
add a comment |
as @danialroseman said, you just need to update the DIRS
in TEMPLATES
variable in settings.py
::
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
### ADD YOUR DIRECTORY HERE LIKE SO:
'DIRS': [ os.path.join(BASE_DIR, 'templates')],
...
now create a directory templates
in the project's root folder and put the base.html
file in it.
This is the correct way
– simple_human
Nov 21 '18 at 16:22
add a comment |
as @danialroseman said, you just need to update the DIRS
in TEMPLATES
variable in settings.py
::
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
### ADD YOUR DIRECTORY HERE LIKE SO:
'DIRS': [ os.path.join(BASE_DIR, 'templates')],
...
now create a directory templates
in the project's root folder and put the base.html
file in it.
as @danialroseman said, you just need to update the DIRS
in TEMPLATES
variable in settings.py
::
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
### ADD YOUR DIRECTORY HERE LIKE SO:
'DIRS': [ os.path.join(BASE_DIR, 'templates')],
...
now create a directory templates
in the project's root folder and put the base.html
file in it.
edited Nov 21 '18 at 15:14
answered Nov 21 '18 at 15:04
V.k. Sulaiman
112
112
This is the correct way
– simple_human
Nov 21 '18 at 16:22
add a comment |
This is the correct way
– simple_human
Nov 21 '18 at 16:22
This is the correct way
– simple_human
Nov 21 '18 at 16:22
This is the correct way
– simple_human
Nov 21 '18 at 16:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414298%2fstandard-for-application-specific-template-directories-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown