37
Different question but same answer:
My usual layout for a django site is:
projects/ templates/ common/ local/
Where:
- projects contains your main project and any others
- common contains things you may share across sites, or are at least not project-specific, like if you need to download django-profile and django-registration rather than having it directly in python/site-packages
- templates contains just that
- local contains things that are going to be specific to the current machine, so that you can have properly separated data, like database location and password – I then soft-link the machine-specific versions (say “machine1-localconfig.py”) to local/localconfig.py and then can “import localconfig” in settings.py
- I generally put middleware that’s project-specific inside a project, and middleware that’s not project-specific in common/middleware/
- make sure to add the templates directory to the right place in settings (or most probably, localconfig.py and then import it in settings), and makse sure to add the projects, common, and local directories to your PYTHONPATH.
28
OK, after reading the comments and answer here I’ve decided to create a directory called “common/util/” inside my project directory. Within that I have a file “__ init__.py” where I have my little helper functions.
I guess if the file gets too big, I’ll then split out the functions into individual .py files in common. So now, my project structure looks like this. Please correct if I’m making any poor choices, I’m early enough in development that I can fix it now while it is still easy to do so!
myproject/ (Django project)
common/
util/
__init__.py (helper functions)
middleware/ (custom middleware)
templates/ (project templates)
myapp/
fixtures/ (initial data to load)
migrations/ (south migrations)
urls/
views/
admin.py
forms.py
models.py
test.py
public/ (static stuff not served by Django)
media/
css/
img/
js/
lib/
- [Django]-Celery missed heartbeat (on_node_lost)
- [Django]-How to convert JSON data into a Python object?
- [Django]-ReactJS with Django – real usage
5
Here is another way to do it:
If the utility functions can be a stand-alone module
and you are using a virtualenv environment for your django app
then you can bundle the functionality as a package and install it in your virtualenv.
This makes it easy to import where ever you need it in your django app.
- [Django]-History of Django's popularity
- [Django]-How to create user from django shell
- [Django]-Model not showing up in django admin
4
It depends whether the functions are project or app specific.
The other answers are already addressing where to place it for project specific functions. More precisely, in a folder called common
in the root of the project.
If we are talking about app specific, then I’d simply place it inside of the app in a file named utils.py
, like
myproject/ (Django project)
common/
util/
__init.py__ (project app specific functions)
myapp/
migrations/ (myapp migrations)
utils.py (myapp specific functions)
views.py
admin.py
forms.py
models.py
test.py
- [Django]-Django Rest framework, how to include '__all__' fields and a related field in ModelSerializer ?
- [Django]-Target WSGI script cannot be loaded as Python module
- [Django]-MySQL "incorrect string value" error when save unicode string in Django