tags:

views:

46

answers:

2

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

+1  A: 

If you're doing the same things over and over and over, then you should do them in a separate module and just import that module.

Ignacio Vazquez-Abrams
+1  A: 

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.

mipadi
view directory have [__init__.py] maybe it's possible to place this line in there, so that every view would "see" those imports?
holms
You'd still have to do something like `from views import *`.
mipadi
ok i just created imports dir and views.py in it, it works. thnx =)
holms