Standardizing and including some imports - convention?
I notice that I import many of the same imports into nearly every view (render_to_re开发者_StackOverflow社区sponse, simplejson, HttpResponseRedirect, and a couple others).
It strikes me as WET to have these same few lines at the top of every view.
Is it conventional to create an include with these imports and include in many views?
If you mean something like:
# utils.py
import django.shortcuts
import simplejson
render = django.shortcuts.render_to_response
asjson = simplejson.loads
tojson = simplejson.dumps
# views.py
import utils
def myview ( request, ... ):
# ...
stuff = utils.asjson(some_presumable_json_formatted_data)
# ...
return utils.render(template_name, ...)
I have no idea if it's conventional, but I do that all the time. It's shorter to type, easier to maintain and just plain DRY.
精彩评论