Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9120469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:38:04+00:00 2026-06-17T05:38:04+00:00

I try my best to write reusable Django apps. Now I’m puzzled how to

  • 0

I try my best to write reusable Django apps. Now I’m puzzled how to put them all together to get the final project.

Here is an example of what I mean:
I have a picture app that stores, resizes and displays images. Also I have a weblog app that stores, edits and displays texts. Now I want to combine these two to show blog posts with images.

To do that I could put foreign key fields in the blog to point at pictures. But then the blog could not be used without the picture app. Also I could create a third app, which is responsible to connect both.

What is the ‘best practice’ way of doing it ?

EDIT: Thank you for your very good answers, but I’m still looking for more practical example of how to solve this problem. To complete my example: Sometimes it would be nice to use the blog app without the picture app. But if I hard code the dependency it is no longer possible. So how about 3rd app to combine both ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:38:06+00:00Added an answer on June 17, 2026 at 5:38 am

    Introduction talk in the bottom of the answer (more straight to the answer). I will assume that you have one app for text handling called Text and one app for picture handling called Pictures and a third app for blogging called Blog.

    Big picture

    You will need to study the manual about the template language for python programmers. The idea is that each thing is in its own app and that you have a third app that connects everything. The apps should then supply its models and views as you like them (just remember to keep you focus on what the app should do) and also supply a set of templatetags.

    How to make inclusion tags

    Make inclusion tags and it is really easy! It will remind you of writing normal views.

    Create a directory templatetags in you app folder. Also create a __init__.py file in this templatetags (so the directory becomes a python package).

    Then create a python file. The name is important, you will use this in {% load xyz %} in the templates that will use your app. For instance if call the file picturestags.py, you will call
    {% load picturestags %} in all templates that will use it.

    First in the file add some politics you need not to think much about, just include this before anything else:

    from django.template import Library
    register = Library()
    

    Then add the tags by defining functions with the same name as your tag. I will call it display_picture in the example and it will take one argument url. The function should create a dictionary that you will use in a template. My example will just display the picture the url is pointing to.

    @register.inclusion_tag('pictures/display_picture.html')
    def display_picture(url):
        return {'picture': url}
    

    Create the path templates/pictures in your app and create the file display_picture.html inside containing:

    <img src="{{ picture }}" />
    

    As you probably understand, the @register makes this a tag, what is inside the dictionary display_picture returns are what you can use in display_picture.html. Very much like your normal view functions.

    In the end you will end up with these files:

    pictures/
        __init__.py
        models.py
        views.py
        tests.py
        templates/
            pictures/
                display_picture.html
        templatetags/
            picturetags.py
    

    That is all you need to add to your Picture app. To use this in your Blog app, you need to add Pictures to your INSTALLED_APPS. Then in the templates, where you need to use your own newly home baked tag first load it: {% load picturestags %} then just add the tag {% display_picture https://www.google.com/intl/sv_ALL/images/logos/images_logo_lg.gif %} like this:

    {% load picturestags %}
    <html>
        <body>
            {% display_picture https://www.google.com/intl/sv_ALL/images/logos/images_logo_lg.gif %}
        </body>
    </html>
    

    Results

    This is just a small example but you can see that it is very easy to expand this. Your blog could connect the Text and Pictures app by importing their models and foreign key them. There is you connection Text and Pictures for a certain blog post. Your blog_post.html-template could look like (simplified):

    {% load picturestags %}
    {% load texttags %}
    <html>
        <body>
            <h1>{{ subject }}</h1>
            <div class="article">{% article_to_html articleid %}</div>
            <div class="article_picture">{% display_picture %}</div>
        </body>
    </html>
    

    Notice that only the Blog has dependencies and it is dependencies it should have (no blog without text and pictures…but pictures can live without text). The look and placement should be and can be controlled by CSS and DIV/SPAN-tags. In this way you can take your Picture app and give it to someone who has no idea about Text app and use it, displaying pictures in different ways probably without ever touching your code!

    Inclusion tags is the only thing I know of since I just learned this yesterday. I think it is a convenience provided by Django to make life simple. On the documentation page there are a whole lot more (including how to make “real” tags the hard way without “shortcuts”). So if you find this method to limited, read the documentation…it has lots of examples. It also discusses how to make filters, simple_tags, thread considerations and other advanced stuff.

    Introduction

    I had this exactly this problem as you and I also wanted something different than the answers I read (I don’t say the answers was bad, they helped me learn a lot and gave me insights, but I wanted this I am writing now). I managed to figure something out that is not very obvious, thanks to your question and definitely thanks to Stack Overflow so this is my contribution back even to a half year old question that is probably abandoned (might help a googler or two)!

    I also got a lot of inspiration from Google Tech Talk Reusable Apps. In the end (43 minutes) he mentions some good examples like django-tagging which is what he says a model for how to write reusable apps. That gave me the idea for all this, because that is the way django-tagging solves this very problem we had/have.

    Now after I written all this (took an hour), I feel for the first time that I might contribute instead of just google and follow what others are doing or complaining that others are not writing how I need to do things. For the first time I am taking my responsibility of writing my view so others can google this (just had to write this paragraph 🙂 because it feels really great, even if it might be shredded to pieces or ignored and forgotten).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a more elegant way to write this? try: author = Author.objects.get
Take Django for instance, in manage.py : try: import settings except ImportError: sys.stderr.write(Error: Can't
I am new to Mathematica. I will try to do my best to write
I'll try my best to explain how I'm trying to set up this system.
Its hard to explain this so I'll try my best. Is it possible to
This a little hard for me to explain, but I'll try my best. I'm
I will try and word this question the best I can, for it is
I'll try to explain this the best I can. I have a drop down
I'm going to try to explain this best I can I will provide more
I'm try to think of the best way to cache a very large collection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.