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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:06:27+00:00 2026-05-24T22:06:27+00:00

Technically not a django question, more a python question. In urls.py I’ve got the

  • 0

Technically not a django question, more a python question.

In urls.py I’ve got the following:

urlpatterns = patterns('locate.views',
    url(r'^',  'index.index'),
)

And a directory structure like this:

locate/
  views/
    __init__.py
    index.py      #  where there is "def index(request) : ...."

What I would like to do is avoid having to say “index.index”, but rather have things flatten out a bit. Thus yielding:

urlpatterns = patterns('locate.views',
    url(r'^',  'index'),
)

This of course is quite possible if I make __ init __.py contain:

from .index import index
...

But after the 99th time of doing that it would be nice to have it automated. I’ve gotten close with some poking around with __ import __ and the like but wondering if anybody else has a working code fragment and/or a better way to handle this pattern in django.

Update Used version of Amit’s code:

this is in the views/__init__.py file (which will be pulled to a library shortly):

from glob import glob1
from types import FunctionType
import os

def import_views(dirname, views_prefix='') :
    for filename in glob1(dirname, '*.py'):
        if filename == '__init__.py':    # I assume you don't want that
            continue

        module_name = os.path.basename(filename).split('.py')[0]

        # You might need to change this, depending on where you run the file:
        imported_module = __import__("%s.%s" % (__package__, module_name), fromlist=[module_name,])

        idict = imported_module.__dict__

        for method_name in idict.get('__all__', idict.keys()):
            method = idict[method_name]
            if not isinstance(method, FunctionType):
                continue
            if views_prefix and not method_name.startswith(views_prefix):
                continue
            globals()[method_name] = method

import_views(os.path.dirname(__file__))
  • 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-24T22:06:30+00:00Added an answer on May 24, 2026 at 10:06 pm

    I believe that importing through other files is somewhat awkward, and that all views should be imported directly – like the way you’re doing now.
    However, you can create a views.py file and import all relevant view methods from there, the dir structure will remain the same, only you’ll add a views.py file under the views/ dir. The code in the file itself should be something like:

    from .index import index
    from .other_view import other_view_method
    ...
    

    then the code in your urls.py file:

     urlpatterns = patterns('locate.views',
        url(r'^',  'index.index'), )
    

    will turn into:

    urlpatterns = patterns('locate.views.views',
        url(r'^',  'index'),
    )
    

    However, if you still want to run over all your *.py files and get all view methods from them, you can create a loader file that runs first and loads all views, the code should be something as follows:

    from glob import glob1
    from types import FunctionType
    
    VIEW_METHOD_PREFIX = ''    # Whatever you like here, I suggest you use something
    VIEWS_DIR          = 'views'    # Where you views are
    
    def import_views():
    
        for filename in glob1(VIEWS_DIR, '*.py'):
            if filename == '__init__.py':    # I assume you don't want that
                continue
    
            module_name = os.path.basename(filename).split('.py')[0]
    
            # You might need to change this, depending on where you run the file:
            imported_module = __import__(
                module_name, fromlist=[module_name,])  
    
            for method_name, method in imported_module.__dict__.iteritems():
                if not isinstance(method, FunctionType):
                    continue
                if not method_name.startswith(VIEW_METHOD_PREFIX):
                    continue
                globals()[method_name] = method
    

    Then in urls.py you add:

    import_views()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is not technically a programming question but it does specifically relate to a
HI, O.K, not technically a programming question but it's still relevent for this forum
Ok, not technically a programming question but I want a plugin for my WordPress
This is not technically a programming question but perhaps someone can help me. Yes,
This is not a technical question, but given that there are a few iPhone
This is not a really technical question but is required for a system I
I've been learning more about Python recently, and as I was going through the
Technically this is my first try in nodejs and frankly I am not sure
I have three tables in MySQL that are related, yet not technically linked to
In the following example, the word Test is not clickable in Internet Explorer, even

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.