Table of Contents
- Introduction
- Audience
- Prerequisites
- Let’s Start Project
- Setup Directory Structure
- Setup Custom User app
- Setting Environment Variables
- FAQ
1. Introduction
Django is a very popular Python Web framework that encourages rapid development and clean, pragmatic design. Its free and Open Source. Some of the busiest sites on the Web leverage Django’s ability to quickly and flexibly scale.
Today will discuss about Django setup and directory structure which helps to scale and maintain the project in the long run.
2. Audience
- Who have basic knowledge of Django.
- Who desire to learn advance Django.
- Developer/Professional/Student who wants to scale Django project.
- Company to start Enterprise Web application with Django.
- Of Course! Who loves Django.
3. Prerequisites
- Mac/Linux/Windows with Python Installed(Preferred Latest Version with Pyenv).
- Pipenv/Pip package manager.
- Github Repository (Optional) or any other version control tool.
Verify setup
4. Lets’s Start Project
I believe system setup is done as prescribed in prerequisites section and verified. Create new directory in workspace.
4.1 Lets create virtual environment using pipenv
.
4.2 Install Django package
Below command install latest version of Django.
4.3 Start django project
Congratulations!
You have successfully created project. Check default Django home page at http://127.0.0.1:8000
5. Setup Directory Structure
Now we comes to important step of project setup. Before going to change directory structure lets understand why we changing it.
Is it mandatory to change directory structure?
No but its good to be organized. What you think ha?
Why Directory structure is important?
Directory structure is very important for any project. How we plan for Home before constructing like Living room, Kitchen, Bedroom etc… every space has its own importance and purpose. Same in project directory structure each directory has its own importance and purpose.
Directory Structure we going to create and its purposes.
apps
this directory contains all the future apps created usingstartapp
command.config
contains all the configurations and settings related to project.common
all common code, modules or utils. These are the important directories. We can add more as per requirement or need.
Note : create __init__.py
file inside each directory. We use it as Python package.
5.1 Add apps
package inside demo directory.
Lets configure Django to consider apps
directory as parent of all apps.
Open demo/settings.py
and add following line.
5.2 Add config
package. Goto parent directory.
This directory contains the all the settings, urls and wsgi.py related to project.
- Copy all the contents of
demo/settings
toconfig/settings/django.py
- Move
wsgi.py
andurls.py
files toconfig
. - Update BASE_DIR, WSGI_APPLICATION, ROOT_URLCONF
config/settings/django.py
- Update settings module in
manage.py
Run the local server and verify its working correctly.
5.3 Add common
package same as we added apps
and config
. Which contains the common code which is applicable to whole project.
6. Setup Custom User app
Now its time to create custom user app.
The purpose of custom app
- Full control on
User
model. - Full control on
Authentication
andAuthorization
. - Flexibility add new
fields
toUser
model. - Segregate
User
related features/code in one place. - Add more flexibility while developing
API
related toUser
.
6.1 Create custom user app.
6.2 Create User
model.
Add following lines to apps/users/model
6.3 Configure user app.
add following line to config/settings/django.py
6.4 Make Migrations
6.5 Run Migrations
6.6 Verify server is running.
7. Setting Environment Variables.
Purpose of environment variables
- Hide the confidential data like
credentials
,secrete keys
,API tokens/credentials
etc. - Never add credentials in code.
- Flexibility to set credenatials environment
dev/test/production
. - Flexibility to developers to setup in local.
- Never push
credentials
orconfidential
data to versioning tool.
7.1 Install django-environ package.
7.2 Create .env
file in parent directory.
NOTE: Add .env
to .gitignore
. Never push this file to repository.
7.3 Configure djnago-environ
. In demo/settings/django.py
add following.
# Set parent directory for environ
base = environ.Path(__file__) - 4
# Rediang file and export env variables
environ.Env.read_env(env_file=base(".env"))
env = environ.Env()
# update following
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env("SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env("DEBUG")
7.4 Auto export of environment variables
.
Wheneever we activate pipenv shell environment variables
are set.
$ pipenv shell
Loading .env environment variables…
Launching subshell in virtual environment…
. /Users/home/.local/share/virtualenvs/nyasa-api-yFS2yA7M/bin/activate
Hurray!. Setup is done.
Django Setup for Enterprise Applications Repository
Using this repository as template for your new project.
$ mkdir demo
$ cd demo
$ django-admin startproject demo . --template https://github.com/deepakkabbur/django-setup/archive/master.zip
NOTE: Please read Documentation. If your are using it as template
8. FAQ
8.1 Error ImportError: Couldn't import Django
?
activate virtual environment
$ pipenv shell
8.2 Error ModuleNotFoundError: No module named users
Update
BASE_DIR
as mentioned above. Refer to5.2
and6.3