Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Where to put helper functions?

Tags:

python

django

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 within my views?

like image 209
The.Anti.9 Avatar asked Dec 16 '09 04:12

The.Anti.9


People also ask

What is helper Django?

Project description Model helpers are small collection of django functions and classes that make working with models easier. All functions here are compliant with pylint and has test cases with over 95% code coverage.

What is a helper function Python example?

A "helper function" is a function you write because you need that particular functionality in multiple places, and because it makes the code more readable. A good example is an average function. You'd write a function named avg or similar, that takes in a list of numbers, and returns the average value from that list.

What is helper object in Python?

A helper method is a term used to describe some method that is reused often by other methods or parts of a program. Helper methods are typically not too complex and help shorten code for frequently used minor tasks. Using helper methods can also help to reduce error in code by having the logic in one place.


2 Answers

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

from myapp.utils import my_cool_func  def 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

like image 195
Anurag Uniyal Avatar answered Sep 28 '22 02:09

Anurag Uniyal


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 :)

like image 33
jujule Avatar answered Sep 28 '22 04:09

jujule