Standard for application-specific template directories in Django?












1















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?










share|improve this question



























    1















    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?










    share|improve this question

























      1












      1








      1








      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?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 14:29









      TheSodesaTheSodesa

      18910




      18910
























          3 Answers
          3






          active

          oldest

          votes


















          1














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





          share|improve this answer


























          • 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











          • I will update my answer to show this detail.

            – Jonah Bishop
            Nov 21 '18 at 14:54



















          1














          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.






          share|improve this answer































            1














            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.






            share|improve this answer


























            • This is the correct way

              – simple_human
              Nov 21 '18 at 16:22











            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            1














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





            share|improve this answer


























            • 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











            • I will update my answer to show this detail.

              – Jonah Bishop
              Nov 21 '18 at 14:54
















            1














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





            share|improve this answer


























            • 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











            • I will update my answer to show this detail.

              – Jonah Bishop
              Nov 21 '18 at 14:54














            1












            1








            1







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





            share|improve this answer















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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '18 at 14:55

























            answered Nov 21 '18 at 14:41









            Jonah BishopJonah Bishop

            9,05433357




            9,05433357













            • 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











            • 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













            • 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

















            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













            1














            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.






            share|improve this answer




























              1














              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.






              share|improve this answer


























                1












                1








                1







                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.






                share|improve this answer













                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 14:54









                Daniel RosemanDaniel Roseman

                455k41588645




                455k41588645























                    1














                    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.






                    share|improve this answer


























                    • This is the correct way

                      – simple_human
                      Nov 21 '18 at 16:22
















                    1














                    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.






                    share|improve this answer


























                    • This is the correct way

                      – simple_human
                      Nov 21 '18 at 16:22














                    1












                    1








                    1







                    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.






                    share|improve this answer















                    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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 21 '18 at 15:14

























                    answered Nov 21 '18 at 15:04









                    V.k. SulaimanV.k. Sulaiman

                    112




                    112













                    • 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





                    This is the correct way

                    – simple_human
                    Nov 21 '18 at 16:22


















                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    這個網誌中的熱門文章

                    Hercules Kyvelos

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud