360π
Thereβre two kind of Django "projects" that I have in my ~/projects/
directory, both have a bit different structure.:
- Stand-alone websites
- Pluggable applications
Stand-alone website
Mostly private projects, but doesnβt have to be. It usually looks like this:
~/projects/project_name/
docs/ # documentation
scripts/
manage.py # installed to PATH via setup.py
project_name/ # project dir (the one which django-admin.py creates)
apps/ # project-specific applications
accounts/ # most frequent app, with custom user model
__init__.py
...
settings/ # settings for different environments, see below
__init__.py
production.py
development.py
...
__init__.py # contains project version
urls.py
wsgi.py
static/ # site-specific static files
templates/ # site-specific templates
tests/ # site-specific tests (mostly in-browser ones)
tmp/ # excluded from git
setup.py
requirements.txt
requirements_dev.txt
pytest.ini
...
Settings
The main settings are production ones. Other files (eg. staging.py
,
development.py
) simply import everything from production.py
and override only necessary variables.
For each environment, there are separate settings files, eg. production,
development. I some projects I have also testing (for test runner), staging
(as a check before final deploy) and heroku (for deploying to heroku) settings.
Requirements
I rather specify requirements in setup.py directly. Only those required for
development/test environment I have in requirements_dev.txt
.
Some services (eg. heroku) requires to have requirements.txt
in root directory.
setup.py
Useful when deploying project using setuptools
. It adds manage.py
to PATH
, so I can run manage.py
directly (anywhere).
Project-specific apps
I used to put these apps into project_name/apps/
directory and import them
using relative imports.
Templates/static/locale/tests files
I put these templates and static files into global templates/static directory, not inside each app.
These files are usually edited by people, who doesnβt care about project code
structure or python at all. If you are full-stack developer working alone or
in a small team, you can create per-app templates/static directory. Itβs really just a matter of taste.
The same applies for locale, although sometimes itβs convenient to create separate locale directory.
Tests are usually better to place inside each app, but usually there is many
integration/functional tests which tests more apps working together, so global
tests directory does make sense.
Tmp directory
There is temporary directory in project root, excluded from VCS. Itβs used to
store media/static files and sqlite database during development. Everything in
tmp could be deleted anytime without any problems.
Virtualenv
I prefer virtualenvwrapper
and place all venvs into ~/.venvs
directory,
but you could place it inside tmp/
to keep it together.
Project template
Iβve created project template for this setup, django-start-template
Deployment
Deployment of this project is following:
source $VENV/bin/activate
export DJANGO_SETTINGS_MODULE=project_name.settings.production
git pull
pip install -r requirements.txt
# Update database, static files, locales
manage.py syncdb --noinput
manage.py migrate
manage.py collectstatic --noinput
manage.py makemessages -a
manage.py compilemessages
# restart wsgi
touch project_name/wsgi.py
You can use rsync
instead of git
, but still you need to run batch of commands to update your environment.
Recently, I made django-deploy
app, which allows me to run single management command to update environment, but Iβve used it for one project only and Iβm still experimenting with it.
Sketches and drafts
Draft of templates I place inside global templates/
directory. I guess one can create folder sketches/
in project root, but havenβt used it yet.
Pluggable application
These apps are usually prepared to publish as open-source. Iβve taken example
below from django-forme
~/projects/django-app/
docs/
app/
tests/
example_project/
LICENCE
MANIFEST.in
README.md
setup.py
pytest.ini
tox.ini
.travis.yml
...
Name of directories is clear (I hope). I put test files outside app directory,
but it really doesnβt matter. It is important to provide README
and setup.py
, so package is easily installed through pip
.
24π
My answer is inspired on my own working experience, and mostly in the book Two Scoops of Django which I highly recommend, and where you can find a more detailed explanation of everything. I just will answer some of the points, and any improvement or correction will be welcomed. But there also can be more correct manners to achieve the same purpose.
Projects
I have a main folder in my personal directory where I maintain all the projects where I am working on.
Source Files
I personally use the django project root as repository root of my projects. But in the book is recommended to separate both things. I think that this is a better approach, so I hope to start making the change progressively on my projects.
project_repository_folder/
.gitignore
Makefile
LICENSE.rst
docs/
README.rst
requirements.txt
project_folder/
manage.py
media/
app-1/
app-2/
...
app-n/
static/
templates/
project/
__init__.py
settings/
__init__.py
base.py
dev.py
local.py
test.py
production.py
ulrs.py
wsgi.py
Repository
Git or Mercurial seem to be the most popular version control systems among Django developers. And the most popular hosting services for backups GitHub and Bitbucket.
Virtual Environment
I use virtualenv and virtualenvwrapper. After installing the second one, you need to set up your working directory. Mine is on my /home/envs directory, as it is recommended on virtualenvwrapper installation guide. But I donβt think the most important thing is where is it placed. The most important thing when working with virtual environments is keeping requirements.txt file up to date.
pip freeze -l > requirements.txt
Static Root
Project folder
Media Root
Project folder
README
Repository root
LICENSE
Repository root
Documents
Repository root. This python packages can help you making easier mantaining your documentation:
Sketches
Examples
Database
- [Django]-Django dynamic forms β on-the-fly field population?
- [Django]-What is the difference between cached_property in Django vs. Python's functools?
- [Django]-Name '_' is not defined
14π
I donβt like to create a new settings/
directory. I simply add files named settings_dev.py
and settings_production.py
so I donβt have to edit the BASE_DIR
.
The approach below increase the default structure instead of changing it.
mysite/ # Project
conf/
locale/
en_US/
fr_FR/
it_IT/
mysite/
__init__.py
settings.py
settings_dev.py
settings_production.py
urls.py
wsgi.py
static/
admin/
css/ # Custom back end styles
css/ # Project front end styles
fonts/
images/
js/
sass/
staticfiles/
templates/ # Project templates
includes/
footer.html
header.html
index.html
myapp/ # Application
core/
migrations/
__init__.py
templates/ # Application templates
myapp/
index.html
static/
myapp/
js/
css/
images/
__init__.py
admin.py
apps.py
forms.py
models.py
models_foo.py
models_bar.py
views.py
templatetags/ # Application with custom context processors and template tags
__init__.py
context_processors.py
templatetags/
__init__.py
templatetag_extras.py
gulpfile.js
manage.py
requirements.txt
I think this:
settings.py
settings_dev.py
settings_production.py
is better than this:
settings/__init__.py
settings/base.py
settings/dev.py
settings/production.py
This concept applies to other files as well.
I usually place node_modules/
and bower_components/
in the project directory within the default static/
folder.
Sometime a vendor/
directory for Git Submodules but usually I place them in the static/
folder.
- [Django]-Django {% if forloop.first %} question
- [Django]-Missing Table When Running Django Unittest with Sqlite3
- [Django]-AngularJS with Django β Conflicting template tags
8π
As per the Django Project Skeleton, the proper directory structure that could be followed is :
[projectname]/ <- project root
βββ [projectname]/ <- Django root
β βββ __init__.py
β βββ settings/
β β βββ common.py
β β βββ development.py
β β βββ i18n.py
β β βββ __init__.py
β β βββ production.py
β βββ urls.py
β βββ wsgi.py
βββ apps/
β βββ __init__.py
βββ configs/
β βββ apache2_vhost.sample
β βββ README
βββ doc/
β βββ Makefile
β βββ source/
β βββ *snap*
βββ manage.py
βββ README.rst
βββ run/
β βββ media/
β β βββ README
β βββ README
β βββ static/
β βββ README
βββ static/
β βββ README
βββ templates/
βββ base.html
βββ core
β βββ login.html
βββ README
Refer https://django-project-skeleton.readthedocs.io/en/latest/structure.html for the latest directory structure.
- [Django]-How can I get tox and poetry to work together to support testing multiple versions of a Python dependency?
- [Django]-Django limit_choices_to for multiple fields with "or" condition
- [Django]-How to server HTTP/2 Protocol with django
4π
Here is what I follow on My system.
-
All Projects: There is a projects directory in my home folder i.e.
~/projects
. All the projects rest inside it. -
Individual Project: I follow a standardized structure template used by many developers called django-skel for individual projects. It basically takes care of all your static file and media files and all.
-
Virtual environment: I have a virtualenvs folder inside my home to store all virtual environments in the system i.e.
~/virtualenvs
. This gives me flexibility that I know what all virtual environments I have and can look use easily
The above 3 are the main partitions of My working environment.
All the other parts you mentioned are mostly dependent on project to project basis (i.e. you might use different databases for different projects). So they should reside in their individual projects.
- [Django]-Whats the difference between using {{STATIC_URL}} and {% static %}
- [Django]-Cron and virtualenv
- [Django]-How do I use an UpdateView to update a Django Model?
3π
You can use https://github.com/Mischback/django-project-skeleton repository.
Run below command:
$ django-admin startproject --template=https://github.com/Mischback/django-project-skeleton/archive/development.zip [projectname]
The structure is something like this:
[projectname]/ <- project root
βββ [projectname]/ <- Django root
β βββ __init__.py
β βββ settings/
β β βββ common.py
β β βββ development.py
β β βββ i18n.py
β β βββ __init__.py
β β βββ production.py
β βββ urls.py
β βββ wsgi.py
βββ apps/
β βββ __init__.py
βββ configs/
β βββ apache2_vhost.sample
β βββ README
βββ doc/
β βββ Makefile
β βββ source/
β βββ *snap*
βββ manage.py
βββ README.rst
βββ run/
β βββ media/
β β βββ README
β βββ README
β βββ static/
β βββ README
βββ static/
β βββ README
βββ templates/
βββ base.html
βββ core
β βββ login.html
βββ README
- [Django]-Django REST Framework: how to substitute null with empty string?
- [Django]-How to access request body when using Django Rest Framework and avoid getting RawPostDataException
- [Django]-No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model