Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Django 101

Boris Feld
September 28, 2016

Django 101

Boris Feld

September 28, 2016

More Decks by Boris Feld

Other Decks in Programming


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

  2. ABOUT ME • Python Dev / sqreen.io • DevOps •

    ! lothiraldan • [email protected]

  4. 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.
  5. BEFORE ALL http://www.commitstrip.com/ fr/2016/09/09/the-mistakes- of-youth/


  7. - Boris FELD « Code is a side-product of application.

    Communication skills are much more valuable than coding one. »
  8. FORMAT • https://docs.djangoproject.com/fr/1.10/intro/ • Github project • At least a

    commit at each part
  9. GIT TIP • Use https://gist.github.com/Lothiraldan/ 0dcac9ad04752ba39ab15300ef4da3d6 for .gitignore

  10. 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.
  11. DJANGO • Most popular Python web framework. • First version

    in July 2005. • The framework was named after guitarist Django Reinhardt.
  12. DJANGO TUTORIAL https://docs.djangoproject.com/fr/1.10/intro/tutorial01/

  13. CONCEPT HTTP REQUEST GET / HTTP/1.1 Host: 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


  16. 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 <!DOCTYPE html> <html lang="en"><head> 1. Version de HTTP 2. Status code (200 OK) 3. Headers 4. Body
  17. 200 OK

  18. 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/
  19. DJANGO TUTORIAL https://docs.djangoproject.com/fr/1.10/intro/tutorial02/ Don’t touch the DATABASE in settings.py file

  20. 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.
  21. 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)
  22. 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!
  23. DJANGO TUTORIAL https://docs.djangoproject.com/fr/1.10/intro/tutorial03/ Challenge: Create a poll from the CLI

    and display it.
  24. CONCEPT TEMPLATING Separation of concerns between data retrieval, request dispatching

    and presentation.

  26. CONCEPT MODEL • Responsible for accessing the data • Expose

    a nice and friendly way to do so
  27. CONCEPT CONTROLLER • Process the HTTP Request • Call the

    model for getting data • Choose an acceptable view
  28. CONCEPT VIEW • Format the data for the user in

    one format • It’s usually an HTML page
  29. - CONCEPT Unix Philosophy « Do One Thing and Do

    It Well »
  30. CONCEPT SEPARATION OF CONCERNS • Why separating the responsibilities? •

    Easier to maintain • Easier to replace
  31. WHAT BENEFITS? • For the model ? • For the

    view ? • For the controller ?
  32. 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.
  33. DJANGO TUTORIAL https://docs.djangoproject.com/fr/1.10/intro/tutorial04/ Challenge: Write custom ListView and DetailView.

  34. ACCESS HTTP REQUEST POST /polls/1/vote/?foo=bar HTTP/1.1 Host: Connection: keep-alive

    csrfmiddlewaretoken=…&choice=2 path method GET POST
  35. CONCEPT CRUD • Most applications behavior can be simplified to:

    • Create • Read • Update • Delete
  36. 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.
  37. DJANGO TUTORIAL Skip https://docs.djangoproject.com/fr/1.10/intro/tutorial05/ But read it one day!

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