Slide 1

Slide 1 text

DJANGO 101 LES MINES - NANCY Boris Feld - 28/09/2016

Slide 2

Slide 2 text

ABOUT ME • Python Dev / sqreen.io • DevOps • ! lothiraldan • [email protected]

Slide 3

Slide 3 text

PRINCIPLES

Slide 4

Slide 4 text

GOODWILL PRINCIPLE • Everyone did its best in the the limits of their knowledge and ability at a certain time • Everyone has a life outside this classroom but will do his best for this not to interfere with the class. • Every assessment has a goal: identifying room for improvement.

Slide 5

Slide 5 text

BEFORE ALL http://www.commitstrip.com/ fr/2016/09/09/the-mistakes- of-youth/

Slide 6

Slide 6 text

EVERYONE IS HERE TO LEARN SOMETHING TODAY

Slide 7

Slide 7 text

- Boris FELD « Code is a side-product of application.
 Communication skills are much more valuable than coding one. »

Slide 8

Slide 8 text

FORMAT • https://docs.djangoproject.com/fr/1.10/intro/ • Github project • At least a commit at each part

Slide 9

Slide 9 text

GIT TIP • Use https://gist.github.com/Lothiraldan/ 0dcac9ad04752ba39ab15300ef4da3d6 for .gitignore

Slide 10

Slide 10 text

WEB FRAMEWORK • Web Frameworks are a toolset designed to support the development of web applications. • Web Frameworks usually provides functions for common tasks like database access, HTML templating or session management. • They speed the development of web applications by taking care of common tasks.

Slide 11

Slide 11 text

DJANGO • Most popular Python web framework. • First version in July 2005. • The framework was named after guitarist Django Reinhardt.

Slide 12

Slide 12 text

DJANGO TUTORIAL https://docs.djangoproject.com/fr/1.10/intro/tutorial01/

Slide 13

Slide 13 text

CONCEPT HTTP REQUEST GET / HTTP/1.1 Host: 127.0.0.1:8000 Connection: keep-alive User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 Hello: Nancy

Slide 14

Slide 14 text

PROCESSING HTTP

Slide 15

Slide 15 text

DJANGO PROCESSING

Slide 16

Slide 16 text

CONCEPT HTTP RESPONSE HTTP/1.0 200 OK Date: Sun, 25 Sep 2016 10:58:40 GMT Server: WSGIServer/0.1 Python/2.7.12 X-Frame-Options: SAMEORIGIN Content-Type: text/html 1. Version de HTTP 2. Status code (200 OK) 3. Headers 4. Body

Slide 17

Slide 17 text

200 OK

Slide 18

Slide 18 text

CONCEPT STATUS CODE • Indicates the presence of errors or validity of a response. • https://en.wikipedia.org/wiki/ List_of_HTTP_status_codes • https://http.cat/ • https://httpstatusdogs.com/

Slide 19

Slide 19 text

DJANGO TUTORIAL https://docs.djangoproject.com/fr/1.10/intro/tutorial02/ Don’t touch the DATABASE in settings.py file

Slide 20

Slide 20 text

CONCEPT ORM • Object Relational Mapping is a technique to access relational data through object-oriented code. • It abstract part of the relational complexity required to access data.

Slide 21

Slide 21 text

CONCEPT ORM • Instead of doing: • You can do: cursor = connection.Cursor() cursor.query('SELECT "polls_question"."id", "polls_question"."question_text", "polls_question"."pub_date" FROM "polls_question" WHERE "polls_question"."id" = ?', 1) result = cursor.fetchone() q = Question.objects.get(pk=1)

Slide 22

Slide 22 text

CONCEPT ADMIN INTERFACE • While website often a presentation of the data for the customers (front-end), we generally need one for administrating the website, like resetting a password for an user. • Django comes up with an almost free admin, so enjoy!

Slide 23

Slide 23 text

DJANGO TUTORIAL https://docs.djangoproject.com/fr/1.10/intro/tutorial03/ Challenge: Create a poll from the CLI and display it.

Slide 24

Slide 24 text

CONCEPT TEMPLATING Separation of concerns between data retrieval, request dispatching and presentation.

Slide 25

Slide 25 text

CONCEPT MVC

Slide 26

Slide 26 text

CONCEPT MODEL • Responsible for accessing the data • Expose a nice and friendly way to do so

Slide 27

Slide 27 text

CONCEPT CONTROLLER • Process the HTTP Request • Call the model for getting data • Choose an acceptable view

Slide 28

Slide 28 text

CONCEPT VIEW • Format the data for the user in one format • It’s usually an HTML page

Slide 29

Slide 29 text

- CONCEPT Unix Philosophy « Do One Thing and Do It Well »

Slide 30

Slide 30 text

CONCEPT SEPARATION OF CONCERNS • Why separating the responsibilities? • Easier to maintain • Easier to replace

Slide 31

Slide 31 text

WHAT BENEFITS? • For the model ? • For the view ? • For the controller ?

Slide 32

Slide 32 text

WHAT BENEFITS? • For the model ? • Other sources of data (file system, nosql, cache, …) • For the view ? • Other formats of presentation (json, xml, cvs, …) • For the controller ? • Can be left untouched when other parts change.

Slide 33

Slide 33 text

DJANGO TUTORIAL https://docs.djangoproject.com/fr/1.10/intro/tutorial04/ Challenge: Write custom ListView and DetailView.

Slide 34

Slide 34 text

ACCESS HTTP REQUEST POST /polls/1/vote/?foo=bar HTTP/1.1 Host: 127.0.0.1:8000 Connection: keep-alive csrfmiddlewaretoken=…&choice=2 path method GET POST

Slide 35

Slide 35 text

CONCEPT CRUD • Most applications behavior can be simplified to: • Create • Read • Update • Delete

Slide 36

Slide 36 text

CONCEPT BUSINESS LOGIC • Most website value reside in their business logic and capabilities to make actions. • Basic CRUD actions are much less valuable but shouldn’t be overlooked, better let Django handle these situations for you. • You better wrote less code but more valuable code.

Slide 37

Slide 37 text

DJANGO TUTORIAL Skip https://docs.djangoproject.com/fr/1.10/intro/tutorial05/ But read it one day!

Slide 38

Slide 38 text

DJANGO TUTORIAL https://docs.djangoproject.com/fr/1.10/intro/tutorial06/