getting-started.mo
Owner: Tycho Tatitscheff
Note: Please create an issue or even a Pull Request with your feedback, typo correction.
Context
During this standard, we will take the example of a book library management app.
We will thus create a MAB project (BAM
, reversed) and a Library app containing a Book
and a Author
Models.
We will create an Admin using Django Admin Framework and a rest API, using Django Rest Framework.
Prerequisites (~10 min)
Have HomeBrew installed (
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)
) (~3 min)Have
$PATH
environment variable prioritizing Homebrew package (export PATH=/usr/local/bin:/usr/local/sbin:$PATH
in .bashrc or similar) (~3 min)Have Python 3.6 installed (
brew install python3
): 3.6 is needed for [f"{variable} text" syntax] (https://cito.github.io/blog/f-strings/), if you have version 3.x (x lower then 6, just use the old format "{} text".format(variable) (~2 min)Have virtualenv installed (
pip3 install virtualenv
) (~1 min)If you use Visual Studio Code, read this article (~5 min)
Steps (~35 min)
Important Note: You should commit between each steps, as you have a shippable application at the end of each steps. This will help you reverting if you delete important stuff.
Initialise a new python project (~5 min)
Create a directory for the project:
mkdir ~/Code/django_formation
Go to the directory:
cd ~/Code/django_formation
Init a git directory:
git init
Create a python env for your project
virtualenv -p $(which python3) .venv
Add this python env to project
.gitignore
:echo ".venv" > .gitignore
Activate the virtualenv
source .venv/bin/activate
CHECK 1: You should see the name of the env in front or your terminal prompt
CHECK 2: Python should now points to your env:
CHECK 3: you should have a
.env
and a.git
folder, plus a.gitignore
file
Install django (~1 min)
Install django with pip:
pip install django
Save the dependencies to a lockfile:
pip freeze > requirements.txt
CHECK 1: you should have a
requirements.txt
containing:CHECK 2: django commands should be available
django-admin.py
Create a project and a first application (~4 min)
Start new project MAB (Mobile Application Backend, or BAM reversed):
django-admin.py startproject mab .
(Note the trailing '.' character)Go to
mab
:cd mab
NOTE: this way of organizing app inside project is not the default, but, in a smaller extends, it is the recommended way for django by Two Scoops of Django, the best book in the world about good practise with django.
Create a first app Library (that will store and manage BAM books):
django-admin.py startapp library
Add
'mab.library'
to theINSTALLED_APPS
, in the settings.py (DO NOT REPLACE THE WHOLE FILE)
Go back to root of the project:
cd ~/Code/django_formation
Migrate the database:
python manage.py migrate
NOTE: In order to keep the method of operation simple, we use the built in sqlite adapters. You can read more to change it the beautiful documentation.
Create the django super user:
python manage.py createsuperuser
and fill with a username, an email and avery-secret(™)
password
CHECK:
Run the server:
python manage.py runserver
Access
http://127.0.0.1:8000
and see the "It worked page"Access
http://127.0.0.1:8000/admin
, connect and see the admin page with the username/password created above
Add the first model (~5 min)
NOTE: for editing Python code, I recommend using Pycharm. The debugging and auto complete features helps a lot. There is a community edition that is free to use. That being said, @yann committed on making VSCode great again, even for python. Stay tuned.
Open
mab/library/models.py
in Pycharm (or other, if you still resists).Lets start with a model
Author
: a model derive frommodels.Model
, and for project documentation have a small docstring explaining what it is
Lets add the first fields in the Author models: there is a lot of available fields in
django.db.models
and even more available in platform specifics modules (eg, postgres array or json fields), or in external plugins
NOTE: it is a good practice to add created and modified fields to models, and add a verbose name.
Create auto-magically the migrations:
python manage.py makemigrations
Migrate the database:
python manage.py migrate
CHECK 1:
Launch
python manage.py showmigrations
: you should see a table with django internal migrations and your migration for library appCHECK 2:
Install https://sqlitestudio.pl
Open the migrated sqlite file
See the new table
Complete with other models (~1 min)
Edit the
models.py
file so it looks like:
Make migrations and migrate
Generate the admin (~1 min)
Open admin.py
CHECK:
Access
http://127.0.0.1:8000/admin
, connect and see the admin page
Customize the admin (~3 min)
You can edit the admin to profit from django powerful admin
CHECK:
Access
http://127.0.0.1:8000/admin
, connect and see the admin page
Install REST Framework (~5 min)
Run
pip install djangorestframework
Save the dependencies to a lockfile:
pip freeze > requirements.txt
Add
'rest_framework'
to yourINSTALLED_APPS
setting.
Add django rest framework urls, in
urls.py
(optional)
Write your first rest API (~10 min)
Create a file
rest.py
in the same folder asmodels.py
Copy the following
Add router urls, in
urls.py
Next steps
Troubleshooting
Django documentation is well organized: https://docs.djangoproject.com/en/1.11/
Beware, at least 1/3 of the article are not about Python and really NSFW. Sam et Max blog is really the best resource for python learning in French, especially django.
Last updated