An introduction to django to the tune of git branches.
This repo contains a very basic overview of django 1.11 and utilizes git's tagging to separate steps in basic app creation.
The README.md
, when viewed on github, should guide through each chapter of the various apps created, in order of complexity. Git tags are used to separate the app into various steps of creation.
In the apps
folder, I have separated the following apps:
- Basic django html pages
web_pages
- Web Apps
web_app
- Django ORM
web_db
- Web / API Endpoints
web_endpoint
apps/django_music
is the project setting app. Base URLs are in apps/django_music/urls.py
- Deployment
- virtualenvs
- caching / rate-liming / scaling
- Databases (other than sqlite3) — we're just focused on ORM usage
- Static files
- JavaScript
The instructions were written for a linux development machine.
- Install Python & Git
- Install virtualenv / pip
- Clone the repo
git clone github.com/octaflop/django_music
(ordjango-admin.py startproject django_music
for projects from scratch). - Spin up a virtual environment:
mkvirtualenv djmusic && pip install -r requirements.txt && add2virtualenv `pwd`/apps
- Ensure things are working by running
./manage.py runserver
and opening a browser tolocalhost:8000
git checkout -b web_page
./manage.py startapp web_pages && mv web_pages apps/web_pages
- Add the app to
INSTALLED_APPS
- Edit
apps/django_music/urls.py
- Edit
apps/web_pages/views.py
- Create a templates directory:
mkdir -p apps/web_pages/templates/web_pages/
- Create a template in that directory
- Go to the local URL you set, in our case:
localhost:8000/pages/
- Same steps as 1-8 above, substituting
pages
forapp
- Let's edit the view this time. Note that template variables are just python dictionaries.
- Also edit the template.
- Create a new template and view function:
dynamic.html
. - Add a
<form>
to this template using django forms - Don't forget about
csrf
!
Now we're going to play with Django's ORM and admin: One of the main selling-points of Django vs other Web Libraries.
- Repeat steps 1-8 in
web_pages
- Let's create a few DB models in
models.py
- Now we need migrations for them
- We create the migrations with
./manage.py makemigrations web_db
- And migrate the db with
./manage.py migrate
. Note, this will migrate all apps if you haven't done so already. - Note, if you're using the repo, that we added a
0002
migration to populate with initial data - We make things easier by using
include
for our URLS and adding our own urls.py definitions in the app, instead of the site. - We also use Generic Views for listing.
- And we also add an
admin.py
file for easy management of the DB. - We can visit
localhost:8000/admin
andlocalhost:8000/db/songs
; etc for more views. - Because our urls are named, we can use the
{% url %}
django template tag to directly link to a model's page (or the list page).
Instead of creating new models, we'll use the old ones from web_db
to create Endpoints.
Endpoints are simply api endpoints which can be used by javascript or mobile to make changes to the DB.
- Spin up
web_endpoints
via./manage.py startapp web_endpoints
andmv
it toapps
- Add
web_endpoints
to ourINSTALLED_APPS
list - Ensure that
[rest_framework](http://www.django-rest-framework.org)
has been added toINSTALLED_APPS
- Create serializers for our Album and Song models in
web_endpoints/serializers.py
. - We then use ViewSets and Routers to link everything else up.
- We will also write a custom creation method for the nested serializer.