如何将所有这些导入移动到独立文件 [英] how to move all those imports to standalone file

查看:299
本文介绍了如何将所有这些导入移动到独立文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想像情况:



我有多个视图的视图目录。所有视图都有大约6行与导入 - 在文件的开头。我每次创建新视图时都会很贴心地将这6行排除。



我通常使用所有这些导入。



从django.contrib.auth.models导入的$ pre> 从django.contrib.auth.decorators导入用户
导入login_required,user_passes_test
from django.core.files.base导入ContentFile
从django.core.urlresolvers import reverse
from django.core import paginator
from django.db import connection
from django.db.models import Q
来自django.http import HttpResponseRedirect,Http404,HttpResponse,HttpResponseForbidden
from django.shortcuts import render_to_response,get_object_or_404
from django.template import loader,Context,RequestContext
from django.utils.translation import ugettext作为_

这样的东西..



所以问题是:



如何将所有这些导入移动到独立文件,以便我可以在我创建的每个视图中包含该文件。而不是音调导入,我将有一个文件与所有视图的导入。我不认为这会杀死应用程序或smtng的性能。我会使用这个文件只是为了最常见的进口或smtng像这样..也许这是死的PHP逻辑仍然与我,我知道包括在python是不好的做法..但我需要一个解决方法,这种情况..它得到了我的神经

解决方案

你可以把它们放在一个模块中,说 imports.py ,然后在您的意见中执行此操作:

 从导入导入* 
/ pre>

但是,我认为大多数Python程序员会争辩(我同意),最好在顶部列出您的导入的模块文件,您实际使用它们,就像您已经在做的那样。它可能看起来有点冗长,但是当它们被方便地列在文件的顶部时,可以轻松地追踪外部模块。这也使得它更加明确您正在导入哪些


Imagine situation:

I have view directory with tons of different views. all views have about 6 lines with imports - in the beginning of the file. it's pretty damn hard copy paste those 6 lines every time I create new view.

I usually using all those imports.

from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required, user_passes_test
from django.core.files.base import ContentFile
from django.core.urlresolvers import reverse
from django.core import paginator
from django.db import connection
from django.db.models import Q
from django.http import HttpResponseRedirect, Http404, HttpResponse, HttpResponseForbidden
from django.shortcuts import render_to_response, get_object_or_404
from django.template import loader, Context, RequestContext
from django.utils.translation import ugettext as _

things like that..

SO QUESTION IS:

HOW to move all those imports to standalone file, so that I could include that file in every view i create. instead of tones imports I would have one file with imports for all views. I don't think that this would kill perfomance of application or smtng. I would use that file just for most common imports or smtng like that.. maybe it's damn php logic still with me, I know that includes in python is bad practise.. but I need a workaround for this situation.. it's getting on my nerves

解决方案

You could put them in a module, say imports.py, and then do this in your views:

from imports import *

But I think most Python programmers would argue (and I'd agree) that it's probably better to list your imports at the top of the module file where you actually use them, like you're already doing. It may seem a bit verbose, but it's a lot easier to track down external modules when they're conveniently listed at the top of the file. It also makes it more explicit what you're importing.

这篇关于如何将所有这些导入移动到独立文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆