How to do a reusable plugin in Django, that can be included into a template?












1














How to do a reusable plugin in Django, that can simply be included into a template?



I read several tutorials. They all describe views that return a HttpResponse and extend a base template. That is useful for the main view. But how to set up a simple drop-in view, that just sits in the sidebar of other apps?



I name this requirements:




  1. It must not return a HttpResponse, as this is done by the main view.

  2. It should be able to display a model.

  3. It is easy to include.

  4. It should run out-of-the-box without copying and adjusting the plugin's template first.


Questions:




  1. How does the interface look like and how is this interface used by the project?

  2. How is the technology called?

  3. How is this genre of packages called on PyPi?

  4. Can you name an example package that shows a canonical implementation?


I guess there are tons of answers, yet it is difficult to find them, as my search keywords are not specific enough. So please excuse to bring up this question, that may sound self-evident to the experienced Django developer.










share|improve this question





























    1














    How to do a reusable plugin in Django, that can simply be included into a template?



    I read several tutorials. They all describe views that return a HttpResponse and extend a base template. That is useful for the main view. But how to set up a simple drop-in view, that just sits in the sidebar of other apps?



    I name this requirements:




    1. It must not return a HttpResponse, as this is done by the main view.

    2. It should be able to display a model.

    3. It is easy to include.

    4. It should run out-of-the-box without copying and adjusting the plugin's template first.


    Questions:




    1. How does the interface look like and how is this interface used by the project?

    2. How is the technology called?

    3. How is this genre of packages called on PyPi?

    4. Can you name an example package that shows a canonical implementation?


    I guess there are tons of answers, yet it is difficult to find them, as my search keywords are not specific enough. So please excuse to bring up this question, that may sound self-evident to the experienced Django developer.










    share|improve this question



























      1












      1








      1







      How to do a reusable plugin in Django, that can simply be included into a template?



      I read several tutorials. They all describe views that return a HttpResponse and extend a base template. That is useful for the main view. But how to set up a simple drop-in view, that just sits in the sidebar of other apps?



      I name this requirements:




      1. It must not return a HttpResponse, as this is done by the main view.

      2. It should be able to display a model.

      3. It is easy to include.

      4. It should run out-of-the-box without copying and adjusting the plugin's template first.


      Questions:




      1. How does the interface look like and how is this interface used by the project?

      2. How is the technology called?

      3. How is this genre of packages called on PyPi?

      4. Can you name an example package that shows a canonical implementation?


      I guess there are tons of answers, yet it is difficult to find them, as my search keywords are not specific enough. So please excuse to bring up this question, that may sound self-evident to the experienced Django developer.










      share|improve this question















      How to do a reusable plugin in Django, that can simply be included into a template?



      I read several tutorials. They all describe views that return a HttpResponse and extend a base template. That is useful for the main view. But how to set up a simple drop-in view, that just sits in the sidebar of other apps?



      I name this requirements:




      1. It must not return a HttpResponse, as this is done by the main view.

      2. It should be able to display a model.

      3. It is easy to include.

      4. It should run out-of-the-box without copying and adjusting the plugin's template first.


      Questions:




      1. How does the interface look like and how is this interface used by the project?

      2. How is the technology called?

      3. How is this genre of packages called on PyPi?

      4. Can you name an example package that shows a canonical implementation?


      I guess there are tons of answers, yet it is difficult to find them, as my search keywords are not specific enough. So please excuse to bring up this question, that may sound self-evident to the experienced Django developer.







      django django-templates django-views django-apps






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 13:20

























      asked Nov 11 at 10:41









      Blcknx

      602120




      602120
























          1 Answer
          1






          active

          oldest

          votes


















          2














          If you don't want to change anything in the base template or in any other view, then best to use a Django Middleware. You manipulate the HTML string that is sent to the browser, adding your "sidebar" HTML/CSS/JS to it.



          That way, you can activate your sidebar simply by adding your Middleware to the MIDDLEWARE list in your project's settings.py file.






          share|improve this answer

















          • 1




            That's an interesting approach to inject HTML code. Yet I want to manipulate the base temple, to control where the plugin output goes. I just don't want to manipulate the plugins template by default.
            – Blcknx
            Nov 11 at 12:00






          • 3




            Ah okay, then I misunderstood. If its okay to manipulate the base template, you could insert the plugin as a template tag instead.
            – C14L
            Nov 11 at 12:05






          • 1




            Thank you. This should work. The magic is in the load command. The terminology "tag" is just not that intuitive for a plugin to find it easily. Is there a genre name for this kind of plugins on PyPi? Can you name one, that demonstrates the canonical implementation?
            – Blcknx
            Nov 11 at 12:51






          • 1




            Any app can register template tags. They usually go into a folder templatetags within the app's folder. For example django-allauth provides two tags to display user data in templates.
            – C14L
            Nov 11 at 13:42






          • 1




            To be specific, an inclusion tag is what you need here.
            – Daniel Roseman
            Nov 11 at 15:15











          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%2f53247931%2fhow-to-do-a-reusable-plugin-in-django-that-can-be-included-into-a-template%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          If you don't want to change anything in the base template or in any other view, then best to use a Django Middleware. You manipulate the HTML string that is sent to the browser, adding your "sidebar" HTML/CSS/JS to it.



          That way, you can activate your sidebar simply by adding your Middleware to the MIDDLEWARE list in your project's settings.py file.






          share|improve this answer

















          • 1




            That's an interesting approach to inject HTML code. Yet I want to manipulate the base temple, to control where the plugin output goes. I just don't want to manipulate the plugins template by default.
            – Blcknx
            Nov 11 at 12:00






          • 3




            Ah okay, then I misunderstood. If its okay to manipulate the base template, you could insert the plugin as a template tag instead.
            – C14L
            Nov 11 at 12:05






          • 1




            Thank you. This should work. The magic is in the load command. The terminology "tag" is just not that intuitive for a plugin to find it easily. Is there a genre name for this kind of plugins on PyPi? Can you name one, that demonstrates the canonical implementation?
            – Blcknx
            Nov 11 at 12:51






          • 1




            Any app can register template tags. They usually go into a folder templatetags within the app's folder. For example django-allauth provides two tags to display user data in templates.
            – C14L
            Nov 11 at 13:42






          • 1




            To be specific, an inclusion tag is what you need here.
            – Daniel Roseman
            Nov 11 at 15:15
















          2














          If you don't want to change anything in the base template or in any other view, then best to use a Django Middleware. You manipulate the HTML string that is sent to the browser, adding your "sidebar" HTML/CSS/JS to it.



          That way, you can activate your sidebar simply by adding your Middleware to the MIDDLEWARE list in your project's settings.py file.






          share|improve this answer

















          • 1




            That's an interesting approach to inject HTML code. Yet I want to manipulate the base temple, to control where the plugin output goes. I just don't want to manipulate the plugins template by default.
            – Blcknx
            Nov 11 at 12:00






          • 3




            Ah okay, then I misunderstood. If its okay to manipulate the base template, you could insert the plugin as a template tag instead.
            – C14L
            Nov 11 at 12:05






          • 1




            Thank you. This should work. The magic is in the load command. The terminology "tag" is just not that intuitive for a plugin to find it easily. Is there a genre name for this kind of plugins on PyPi? Can you name one, that demonstrates the canonical implementation?
            – Blcknx
            Nov 11 at 12:51






          • 1




            Any app can register template tags. They usually go into a folder templatetags within the app's folder. For example django-allauth provides two tags to display user data in templates.
            – C14L
            Nov 11 at 13:42






          • 1




            To be specific, an inclusion tag is what you need here.
            – Daniel Roseman
            Nov 11 at 15:15














          2












          2








          2






          If you don't want to change anything in the base template or in any other view, then best to use a Django Middleware. You manipulate the HTML string that is sent to the browser, adding your "sidebar" HTML/CSS/JS to it.



          That way, you can activate your sidebar simply by adding your Middleware to the MIDDLEWARE list in your project's settings.py file.






          share|improve this answer












          If you don't want to change anything in the base template or in any other view, then best to use a Django Middleware. You manipulate the HTML string that is sent to the browser, adding your "sidebar" HTML/CSS/JS to it.



          That way, you can activate your sidebar simply by adding your Middleware to the MIDDLEWARE list in your project's settings.py file.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 11:33









          C14L

          7,20031736




          7,20031736








          • 1




            That's an interesting approach to inject HTML code. Yet I want to manipulate the base temple, to control where the plugin output goes. I just don't want to manipulate the plugins template by default.
            – Blcknx
            Nov 11 at 12:00






          • 3




            Ah okay, then I misunderstood. If its okay to manipulate the base template, you could insert the plugin as a template tag instead.
            – C14L
            Nov 11 at 12:05






          • 1




            Thank you. This should work. The magic is in the load command. The terminology "tag" is just not that intuitive for a plugin to find it easily. Is there a genre name for this kind of plugins on PyPi? Can you name one, that demonstrates the canonical implementation?
            – Blcknx
            Nov 11 at 12:51






          • 1




            Any app can register template tags. They usually go into a folder templatetags within the app's folder. For example django-allauth provides two tags to display user data in templates.
            – C14L
            Nov 11 at 13:42






          • 1




            To be specific, an inclusion tag is what you need here.
            – Daniel Roseman
            Nov 11 at 15:15














          • 1




            That's an interesting approach to inject HTML code. Yet I want to manipulate the base temple, to control where the plugin output goes. I just don't want to manipulate the plugins template by default.
            – Blcknx
            Nov 11 at 12:00






          • 3




            Ah okay, then I misunderstood. If its okay to manipulate the base template, you could insert the plugin as a template tag instead.
            – C14L
            Nov 11 at 12:05






          • 1




            Thank you. This should work. The magic is in the load command. The terminology "tag" is just not that intuitive for a plugin to find it easily. Is there a genre name for this kind of plugins on PyPi? Can you name one, that demonstrates the canonical implementation?
            – Blcknx
            Nov 11 at 12:51






          • 1




            Any app can register template tags. They usually go into a folder templatetags within the app's folder. For example django-allauth provides two tags to display user data in templates.
            – C14L
            Nov 11 at 13:42






          • 1




            To be specific, an inclusion tag is what you need here.
            – Daniel Roseman
            Nov 11 at 15:15








          1




          1




          That's an interesting approach to inject HTML code. Yet I want to manipulate the base temple, to control where the plugin output goes. I just don't want to manipulate the plugins template by default.
          – Blcknx
          Nov 11 at 12:00




          That's an interesting approach to inject HTML code. Yet I want to manipulate the base temple, to control where the plugin output goes. I just don't want to manipulate the plugins template by default.
          – Blcknx
          Nov 11 at 12:00




          3




          3




          Ah okay, then I misunderstood. If its okay to manipulate the base template, you could insert the plugin as a template tag instead.
          – C14L
          Nov 11 at 12:05




          Ah okay, then I misunderstood. If its okay to manipulate the base template, you could insert the plugin as a template tag instead.
          – C14L
          Nov 11 at 12:05




          1




          1




          Thank you. This should work. The magic is in the load command. The terminology "tag" is just not that intuitive for a plugin to find it easily. Is there a genre name for this kind of plugins on PyPi? Can you name one, that demonstrates the canonical implementation?
          – Blcknx
          Nov 11 at 12:51




          Thank you. This should work. The magic is in the load command. The terminology "tag" is just not that intuitive for a plugin to find it easily. Is there a genre name for this kind of plugins on PyPi? Can you name one, that demonstrates the canonical implementation?
          – Blcknx
          Nov 11 at 12:51




          1




          1




          Any app can register template tags. They usually go into a folder templatetags within the app's folder. For example django-allauth provides two tags to display user data in templates.
          – C14L
          Nov 11 at 13:42




          Any app can register template tags. They usually go into a folder templatetags within the app's folder. For example django-allauth provides two tags to display user data in templates.
          – C14L
          Nov 11 at 13:42




          1




          1




          To be specific, an inclusion tag is what you need here.
          – Daniel Roseman
          Nov 11 at 15:15




          To be specific, an inclusion tag is what you need here.
          – Daniel Roseman
          Nov 11 at 15:15


















          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.





          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247931%2fhow-to-do-a-reusable-plugin-in-django-that-can-be-included-into-a-template%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