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

  • Home
  • SEARCH
  • 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 730279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:53:37+00:00 2026-05-14T06:53:37+00:00

The title may be a little confusing, but I don’t know how else to

  • 0

The title may be a little confusing, but I don’t know how else to call it.

I would like to create a Django project with a large set of applications you could arbitrary turn on or off using INSTALLED_APPS option in settings.py (you would obviously also need to edit urls.py and run syncdb). After being turned on an app should be able to automatically:

  1. Register it’s content in site-wide search. Luckily django-haystack has this built-in, so it’s not a problem.

  2. Register cron jobs. django-cron does exactly that. Not a problem.

  3. Register a widget that should be displayed on the homepage. The homepage should include a list of boxes with widgets form different applications.

    I thought about inclusion tags, because you can put them anywhere on a page and they control both content and presentation. The problem is I don’t know how to automatically get a list of inclusion tags provided by my applications, and display them one by one on a homepage. I need a way to register them somehow, and then display all registered tags.

  • 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-05-14T06:53:38+00:00Added an answer on May 14, 2026 at 6:53 am

    I’m not sure using inclusion tag is actually your best choice… There’s no easy way, AFAIK, to call template tags dynamically from a template (and that’s not the point of a template tag anyway :-).

    I suppose I can make the following assumptions on your widgets, correct me if I’m wrong :

    • a widget rendering function doesn’t need parameters from the template context
    • at most, it will eventually need the current request object (so it can access user, session, etc…)

    With this, you can think of your widgets as mini-views, returning a string instead of a response:

    def my_widget(request):
        ...
        # Here too, the function control both presentation and content
        return render_to_string('my_widget.html', {'foo': bar})
    

    Now there’s two issues to adress :

    1. How to have a dynamic list of all widget functions for all installed apps, available in the template?
    2. How to have these widget functions called in the template?

    First point:

    An easy way is to rely on a convention. Have a uniformly named list of functions in a template_widgets.py module in all your applications, eg:

    ## myapp/template_widgets.py
    
    def shopping_cart(request):
        # do something with the session/user
        ...
        return render_to_string('myapp/widgets/cart.html', {'cart': cart})
    
    # Another widget func, not defined here
    from myapp.views import another_widget_func
    
    # The list of widget functions declared by this app
    widgets = (shopping_cart, another_widget_func,)
    

    Then, you can load a global list of widgets by looking at INSTALLED_APPS, and have it automatically available in all your templates (using a context processor). Of course, it’s better to load this list lazily to be sure not to waste CPU cycles on building it if you’re not gonna use it.

    ## myproject/context_processors.py
    from django.utils.importlib import import_module
    from django.utils.functional import lazy
    
    def widgets(request):
        def get_all_widgets(request):
            from django.conf import settings
            widgets_list = []
            for app in settings.INSTALLED_APPS:
                try:
                    mod = import_module(app+'.template_widgets')
                    widgets_list.extend(mod.widgets)
                except ImportError:
                    pass
                except AttributeError:
                    # Couldn't find a widgets variable in app.template_widgets module,
                    # probably better to raise a custom exception
                    raise
            return widgets_list
        return {'widgets': lazy(get_all_widgets, list)(request)}
    

    Second point:

    Now, you have the list of widgets available, and lazily loaded, in every templates. A convenient syntax for using it would be something like:

    ## home.html
    ...
    <div id="widgets">
        {% for widget in widgets %}
            <div class="widget">
                {{ widget }}
            </div>
        {% endfor %}
    </div>
    

    But this will not work, {[widget}} is here a callable, that needs a request parameter. Djano doesn’t allow you to call callables with parameters from within a template, so you have to modify a bit the context processor to return a list of (lazily) evaluated widget functions.

    ## myproject/context_processors.py
    ...
            # replace `return widgets_list` with the following
            return map(lambda w: lazy(w, str)(request), widgets_list)
    ...
    

    And voilà, the above template code should now be working.

    Notes:

    • The order of the widgets in the list depends on the orders of the apps in INSTALLED_APPS and in each widgets lists. Up to you to choose the right ordering method for you (using weighting for example, using a dict to access widgets function by names, etc..)
    • Don’t forget to load the context processor, and to always use a RequestContext.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The title may be not exactly, but I don't know how to express it.
The title may seem a little weird but what I find around the internet
the title may not be appropriate because I don't really know how I should
I realize something like this has been asked, but this may be a little
Ok so my question title may have been a little confusing. Here's my example:
Okay the question title may not have made sense... mostly because I don't know
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
OK so that title sucks a little but I could not think of anything
the title may be a little unclear, so here's a screenshot of what I
I think I may know the answer to this question but I'm actually looking

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.