getting-started.mo
Last updated
Last updated
Note: Please create an or even a Pull Request with your feedback, typo correction.
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.
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] (), 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 (~5 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.
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 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
Start new project MAB (Mobile Application Backend, or BAM reversed): django-admin.py startproject mab .
(Note the trailing '.' character)
Go to mab
: cd mab
Create a first app Library (that will store and manage BAM books): django-admin.py startapp library
Add 'mab.library'
to the INSTALLED_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
Create the django super user: python manage.py createsuperuser
and fill with a username, an email and a very-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
Open mab/library/models.py
in Pycharm (or other, if you still resists).
Lets start with a model Author
: a model derive from models.Model
, and for project documentation have a small docstring explaining what it is
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:
Open the migrated sqlite file
See the new table
Edit the models.py
file so it looks like:
Make migrations and migrate
Open admin.py
CHECK:
Access
http://127.0.0.1:8000/admin
, connect and see the admin page
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
Run pip install djangorestframework
Save the dependencies to a lockfile: pip freeze > requirements.txt
Add 'rest_framework'
to your INSTALLED_APPS
setting.
Add django rest framework urls, in urls.py
(optional)
Create a file rest.py
in the same folder as models.py
Copy the following
Add router urls, in urls.py
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 , the best book in the world about good practise with django.
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 .
NOTE: for editing Python code, I recommend using . The debugging and auto complete features helps a lot. There is a community edition that is free to use. That being said, committed on making VSCode great again, even for python. Stay tuned.
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, ), or in external plugins
Install
Django documentation is well organized:
Beware, at least 1/3 of the article are not about Python and really NSFW. is really the best resource for python learning in French, especially django.
Django has a strong community on and