tags:

views:

90

answers:

3

I have a couple of functions that i wrote that I need to use in my django app. Where would I put the file with them and how would i make them callable whithin my views?

+5  A: 

I usually put such app specific helper function in file utils.py and use someting like this

from myapp.utils import my_cool_func

dev view_coolness(request):
    data = my_cool_func(request)
    return render_to_response("xxx.html")

but it depends what you helper does, may be they modify request , the could be part of middleware, so you need to tell what exactly those helper functions do

Anurag Uniyal
It's generally a bad idea to mention the app in the code. Just doing `from utils import my_cool_func` should work without having to re-state the name of the app which you're already in.
Peter Bengtsson
@Peter Bengtsson, may be but how do you import utils in a module in a package e.g. I have views folder with several view modules, and then middleware folders etc
Anurag Uniyal
+1  A: 

If they are related to a specific app, I usually just put them in the related app folder and name the file, 'functions.py'.

If they're not specific to an app, you could just make a 'functions' app folder and place them in there.

monkut
+2  A: 

create a reusable app that include your generic functions so you can share between projects.

use for example a git repo to store this app and manage deployments and evolution (submodule)

use a public git repo so you can share with the community :)

jujule