$30 off During Our Annual Pro Sale. View Details »

Django + Flask

Django + Flask

Configure Flask for Django 1.8.

Tzu-ping Chung

September 08, 2015
Tweet

More Decks by Tzu-ping Chung

Other Decks in Programming

Transcript

  1. Django + Jinja2

    View Slide

  2. Django Templates
    • The Django Template Language
    • How Django loads templates

    View Slide

  3. Django Templates
    • The Django Template Language
    • How Django loads templates
    DTL

    View Slide

  4.  
       {%  for  user  in  users.all  %}  
         
             
               {{  user.username|capitalize  }}  
             
         
       {%  endfor  %}  

    View Slide

  5.  
       {%  for  user  in  users.all  %}  
         
             
               {{  user.username|capitalize  }}  
             
         
       {%  endfor  %}  

    (Template) Tag

    View Slide

  6.  
       {%  for  user  in  users.all  %}  
         
             
               {{  user.username|capitalize  }}  
             
         
       {%  endfor  %}  

    Variable

    View Slide

  7.  
       {%  for  user  in  users.all  %}  
         
             
               {{  user.username|capitalize  }}  
             
         
       {%  endfor  %}  

    Variable + Filter

    View Slide

  8. The DTL
    • Simple syntax with few rules
    • Lightweight extensions
    • Self-contained

    View Slide

  9. But…
    • Awkward DSL
    • No scoped functions
    • Slow with frequent rendering

    View Slide

  10. Django 1.8
    • django.template
    • django.template.backends

    View Slide

  11. TEMPLATE_CONTEXT_PROCESSORS  
    TEMPLATE_DEBUG  
    TEMPLATE_STRING_IF_INVALID  
    TEMPLATE_DIRS  
    TEMPLATE_LOADERS
    The Language
    The System

    View Slide

  12. TEMPLATE_CONTEXT_PROCESSORS  
    TEMPLATE_DEBUG  
    TEMPLATE_STRING_IF_INVALID  
    TEMPLATE_DIRS  
    TEMPLATE_LOADERS
    The Language
    The System

    View Slide

  13. TEMPLATES  =  [  
           {  
                   'BACKEND':  '...',  
                   'DIRS':  [],  
                 'OPTIONS':  {  
                           'context_processors':  [...],  
                           'debug':  False,  
                           'string_if_invalid':  '',  
                   },  
           },  
    ]
    Language-specific Setup
    Template-loading Setup

    View Slide

  14. View Slide

  15. Jinja2
    • Armin Ronacher (aka mitsuhiko)
    • Inspired by the DTL
    • Standalone library
    • More programmer-friendly

    View Slide

  16.  
       {%  for  user  in  users.all()  %}  
         
             
               {{  user.username.toupper()  }}  
             
         
       {%  endfor  %}  
    Function calling

    View Slide

  17.  
       {%  for  user  in  users.get_friends(me)  %}  
         
             
               {{  user.username.toupper()  }}  
             
         
       {%  endfor  %}  

    View Slide

  18. Settings for Jinja2
    {  
           'BACKEND':  (  
                   'django.template.backends.'  
                   'jinja2.Jinja2'),  
           'DIRS':  [],  
           'APP_DIRS':  True,  
    },

    View Slide

  19. demoapp
    ├── jinja2
    │ └── a_jinja2_template.html
    ├── models.py
    ├── templates
    │ └── a_django_template.html
    ├── urls.py
    └── views.py

    View Slide

  20. demoapp
    ├── jinja2
    │ └── a_jinja2_template.html
    ├── models.py
    ├── templates
    │ └── a_django_template.html
    ├── urls.py
    └── views.py
    Configurable (not recommended)

    View Slide

  21. Jinja2 for Django
    • Auto-escape on
    • Custom template loader
    • Debug enhancements
    • Auto-reload
    • Undefined raises exceptions

    View Slide

  22. But
    • Jinja2 is not built (just) for Web
    • Web-related functionalities
    • Django internals

    View Slide

  23. {%  url  'pages:page'  name=page_name  %}  
    {%  static  'base/css/site.min.css'  %}  
    {%  trans  'This  is  my  website!'  %}  
    {%  csrf_token  %}

    View Slide

  24. https://github.com/MoritzS/jinja2-django-tags

    View Slide

  25. {  
           'BACKEND':  ('django.template.backends.'  
                                   'jinja2.Jinja2'),  
           'DIRS':  [],  
           'APP_DIRS':  True,  
           'OPTIONS':  {  
                   'extensions':  [  
                           'jdj_tags.extensions.DjangoStatic',  
                           'jdj_tags.extensions.DjangoI18n',  
                   ]  
           },  
    },

    View Slide

  26. Jinja2 vs DTL
    • Functions vs template tags
    • Methods vs filters
    • Extensions are loaded by project

    View Slide

  27. The Flask Way
    • {%  url_for(endpoint,  **kwargs)  %}
    • Endpoint 'static'
    • {%  get_flashed_messages(...)  %}
    • I18n API (already pretty similar)
    • {{  csrf_token()  }}

    View Slide

  28. To boldly go where no one
    has gone before.

    View Slide