Releases: octaflop/django_music
Releases · octaflop/django_music
Web Endpoints / API Example
4. web_endpoint
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.
Web DB / ORM Example
3. web_db
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)
Web App Example
2. web_app
- Same steps as 1-8 in 01_web_page , 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
!
Web pages Example
1. web_pages
git checkout -b web_page
./manage.py startapp web_pages && mv web_pages apps/web_pages
- 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/