[英]Django sub-directory views and import all files from __init__.py
Technically not a django question, more a python question. 从技术上讲不是Django问题,更多是python问题。
In urls.py I've got the following: 在urls.py中,我有以下内容:
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. 我想做的是避免不得不说“ index.index”,而是让事情变得平淡一些。 Thus yielding:
因此产生:
urlpatterns = patterns('locate.views',
url(r'^', 'index'),
)
This of course is quite possible if I make __ init __.py contain: 如果我使__ init __.py包含以下内容,这当然是有可能的:
from .index import index
...
But after the 99th time of doing that it would be nice to have it automated. 但是在执行完第99次之后,最好将它自动化。 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.
我已经与__ import __之类的东西打交道了,但是想知道是否还有其他人可以使用代码片段和/或更好的方式来处理Django中的这种模式。
Update Used version of Amit's code: 更新 Amit代码的二手版本:
this is in the views/__init__.py
file (which will be pulled to a library shortly): 这在
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__))
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.
但是,您可以创建一个views.py文件并从那里导入所有相关的view方法,dir结构将保持不变,只有在views / dir下添加一个views.py文件。 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: 然后您的urls.py文件中的代码:
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: 但是,如果仍然要运行所有* .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
Then in urls.py you add: 然后在urls.py中添加:
import_views()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.