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 8062037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:32:00+00:00 2026-06-05T10:32:00+00:00

I’m writing small application for my Django project. The idea is to create some

  • 0

I’m writing small application for my Django project. The idea is to create some kind of widgets – small view functions that can be added in templates via inclusion tags to display some aside information (e.g. blog records, quick-links lists, menus etc). I want to create something similar to default templatetag registering mechanism for this purpose. Generally I need the following components:

  • custom decorator to register these view functions
  • some function that will search for specific modules in all installed apps (‘widgets.py’ for now) at startup (possibly called from settings.py)

What I wrote by now looks like this:

# -*- coding: utf-8 -*-


REGISTERED_WIDGETS = []

class Library(object):
    """
    Utility class for registering defined widgets
    """
    def widget(self, view=None, name=None, form_class=None):
        if name is None:
            name = view.__name__
        def decorator(view):
            print 'registering', view
            REGISTERED_WIDGETS.append((view, name, form_class))
            def wrapper(request, *args, **kwargs):
                return view(request, *args, **kwargs)
            return wrapper
        if view is not None:
            return decorator(view)
        else:
            return decorator

def search_widgets():
    """
    Search for 'widgets.py' modules inside installed applications and import them,
    hence initializing its registration
    """
    from django.conf import settings

    for app in settings.INSTALLED_APPS:
        try:
            module = __import__(app + '.widgets')
            print module
        except ImportError:
            pass
        else:
            print 'Imported widgets from ', app

if __name__ == '__main__':
    search_widgets()
    print REGISTERED_WIDGETS

To test it I added this stub widget in widgets.py inside one of my applications:

# -*- coding: utf-8 -*-

from experiments.widgets.base import Library

register = Library()

@register.widget(name='dummy')
def dummy_widget(request):
    pass

I stuck now one silly issue – when run this module I suppose to see REGISTERED_WIDGETS variable filled with found widgets, but it’s empty. Actually I see this output:

>>> %run widgets/base.py
<module 'django' from '/usr/lib/python2.7/dist-packages/django/__init__.pyc'>
Imported widgets from  django.contrib.admin
registering <function dummy_widget at 0x364f320>
<module 'experiments' from '/home/east825/Development/pycharm-experiments/experiments/../experiments/__init__.pyc'>
Imported widgets from  experiments.layout
[]

It seems that I forgot something very important about python shared global variables behavior.
Any advices?

  • 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-05T10:32:02+00:00Added an answer on June 5, 2026 at 10:32 am

    It could be possible that your module base.py gets imported twice?

    Add a print at the beginning of base.py with a “print ‘base.py executed'” or something.

    edit: or even better, print id(REGISTERED_WIDGETS)

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.