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
Tweet

More Decks by Boris Feld

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

  3. 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.

    View full-size slide

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

    View full-size slide

  5. EVERYONE IS HERE TO LEARN
    SOMETHING TODAY

    View full-size slide

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

    Communication skills are much more valuable than
    coding one. »

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  9. 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.

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  12. 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

    View full-size slide

  13. PROCESSING HTTP

    View full-size slide

  14. DJANGO
    PROCESSING

    View full-size slide

  15. 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

    View full-size slide

  16. 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/

    View full-size slide

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

    View full-size slide

  18. 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.

    View full-size slide

  19. 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)

    View full-size slide

  20. 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!

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  29. 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.

    View full-size slide

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

    View full-size slide

  31. 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

    View full-size slide

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

    View full-size slide

  33. 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.

    View full-size slide

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

    View full-size slide

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

    View full-size slide