Django
Framework
Python Girona 15/11/2017
Django App&API (Manel Clos) + React client (Francesc Arpí)
Què és Django?
- Framework web, codi obert, Python
- Des de finals del 2005
- Documentació!
- Configurable (BD, cache, plantilles...)
- ORM
- Suport a "llarg" termini
- Política canvis, actualitzacions
- Github: 29,565 estrelles
- Github: 1,491 contribuidors
Creació webapp molt senzilla
- Virtualenv
- Pip
- Django
Virtualenv - crear entorn
$ virtualenv venv
New python executable in /.../pygrn/venv/bin/python
Installing setuptools, pip, wheel...done.
$ . venv/bin/activate
(venv) $
Pip: instal·lar Django
(venv) $ pip install django
Collecting django
Downloading Django-1.11.7-py2.py3-none-any.whl (6.9MB)
100% |████████████████████████████████| 7.0MB 163kB/s
Collecting pytz (from django)
Downloading pytz-2017.3-py2.py3-none-any.whl (511kB)
100% |████████████████████████████████| 512kB 1.9MB/s
Installing collected packages: pytz, django
Successfully installed django-1.11.7 pytz-2017.3
Django: crear projecte
(venv) $ django-admin startproject todos_project
(venv) $ cd todos_project/
(venv) $ dir -l
total 8
-rwxr-xr-x 1 manel staff 811 Nov 6 20:12 manage.py
drwxr-xr-x 6 manel staff 204 Nov 6 20:12 todos_project
Django: servidor devel
(venv) $ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 13 unapplied migration(s). Your project may not work
properly until you apply the migrations for app(s): admin, auth,
contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
November 06, 2017 - 19:14:10
Django version 1.11.7, using settings 'todos_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Django
Creació webapp molt senzilla (II)
- Admin
- Migracions
- Superuser
Django: admin
Django: migracions
(venv) $ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
Django: superusuari
(venv) $ python manage.py createsuperuser
Username (leave blank to use 'manel'): admin
Email address:
Password:
Password (again):
Superuser created successfully.
(venv) $ python manage.py runserver
Django: admin dashboard
Django: admin users
Django: admin - edició
Creació webapp molt senzilla (III)
- Aplicació todos
- Vistes
- Plantilles
Django: crear app
(venv) $ python manage.py startapp todos
Django: vista senzilla
Django: urlpatterns app
Django: urlpatterns projecte
Django: vista al navegador
Django: vista amb plantilla
Django: plantilla
Django: installed apps
Django: resultat web
Creació webapp molt senzilla (IV)
- Models
- Models a l'admin
- Errors
- Migracions
Django: models
Django: model a l'admin
Django: admin dashboard
Django: errors en mode debug
Django: crear migracions
(venv) $ python manage.py makemigrations todos
Migrations for 'todos':
todos/migrations/0001_initial.py
- Create model Todo
(venv) $ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, todos
Running migrations:
Applying todos.0001_initial... OK
Django: admin - llistat todos
Django: admin - afegir todo
Django: admin - configuració
class QuestionAdmin(admin.ModelAdmin):
# ...
list_display = ('question_text', 'pub_date')
list_filter = ['pub_date']
search_fields = ['question_text']
Extensió de la webapp per dotar-la d'una petita API REST
- Djando REST Framework
- Serialitzador
- Vista
- Router
Django: instal·lar DRF
(venv) $ pip install djangorestframework
Collecting djangorestframework
Downloading djangorestframework-3.7.3-py2.py3-none-any.whl (1.5MB)
100% |████████████████████████████████| 1.5MB 679kB/s
Installing collected packages: djangorestframework
Successfully installed djangorestframework-3.7.3
Django: serialitzador
Django: viewset
Django: router
Django: installed apps
Extensió de la webapp per dotar-la d'una petita API REST (II)
- API Browser
- Prova de la API
Django: API browser
Django: API Todos list
Django: API Todos add
Django: API Todos - created
Django: API Todos - list
Django: API Todos - detail
Gràcies!
- Preguntes / Dubtes?
Django Framework
By Manel Clos
Django Framework
- 674