2010-10-28 61 views
2

技术上不是Django的问题,更多的是python问题。Django子目录视图并从__init__.py导入所有文件

在urls.py我有以下几点:

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

和目录结构是这样的:

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

我想要做的是避免了不得不说的“指数。指数“,而是让事情平平一点。由此产生:

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

当然这是很可能,如果我做__初始化__.py包含:

from .index import index 
... 

但是这样做,这将是不错的它自动化的第99次以后。我已经用一些关于__ import __之类的东西来关闭它,但是想知道是否有其他人有一个工作代码片段和/或更好的方法来在django中处理这种模式。

更新使用的版本的Amit的代码:

这是在views/__init__.py文件(这将在短期内拉到库):

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__)) 
+0

,我不太清楚你的意思是......你想生成一个__init__.py文件或URL模式还是什么呢? – Joshkunz 2010-10-28 16:30:35

+0

我想要__init__.py将所有* .py文件导入到它的名称空间中。因此,使诸如:locate.views.index有效的函数不是对模块的引用。 – koblas 2010-10-28 16:37:32

+0

我认为这是不好的风格,你应该与'index.index'住在一起。其他任何事情都会在功能和模块之间造成混淆(对于您或他人)。 – katrielalex 2010-10-28 16:46:04

回答

1

我相信,导入通过其他文件,有些尴尬,所有的意见都应该直接导入 - 就像你现在所做的那样。 但是,您可以创建一个views.py文件并从那里导入所有相关的视图方法,dir结构将保持不变,只有您将在views/dir下添加views.py文件。该文件中的代码本身就应该是这样的:

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

然后在你的urls.py文件中的代码:

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

会变成:

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

但是,如果你仍然想要运行所有的* .py文件并从它们中获取所有的视图方法,你可以创建一个首先运行并加载所有视图的加载器文件,代码应该如下所示:

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 

然后在urls.py添加:

import_views() 
相关问题