summaryrefslogtreecommitdiff
path: root/tutorial_5_django_templates/slides.md
blob: 4a149f64d463c4755f3739ecfb516fc4651bbb1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
Tutorial: Create html template in django
===========================================
[Demonstration time: 7 mins (0.805 ~ 81%) | Total time: 8 mins 42 s]

Slide 1 [00:08 | 00:08]
------------
Title Slide
**Creating html template in django**

Slide 2 [00:12 | 00:20]
--------------

**Learning Objectives**

In this tutorial, we will learn to;
  - Create a django html template

Slide 3 [00:11 | 00:31]
---------------

**System Requirements**
  - Ubuntu 16.10
  - Python 3.5 or higher version
  - python3.4-venv
  
Slide 4 [00:11 | 00:42]
---------------

**Pre-requisites**

In order to follow this tutorial, you need to know;
  - how to create views in django
  - If not, see the relevant django tutorial on http://spoken-tutorial.org

Slide 5 [00:18 | 01:00]
------------
**What is a Django Template**
  - We have hardcoded the output in the view
  - What if we want the displayed html to change dynamically?
    - Use a 'template' HTML file
  - Template HTML files contain Django template tags that act like variables
  - Django replaces these tags in the HTML file with dynamic data obtained from views

 Demonstration [04:00 | 05:00]
----------------
**How to create a template**

  - Create a directory called *templates* in blog directory (/blog/templates)
    - cd blogs
    - mkdir templates
  - Create a directory called *blog* within the *templates* directory
    - cd templates
    - mkdir blog
    
In this directory create an *blogs.html* file and add the below code
  
    # /blog/templates/blog/blogs.html
    {% if articles %}
        <ul>
        {% for article in articles %}
            <li>{{ article.title }} | Blog: {{ article.blog.name}}</li>
        {% endfor %}
        </ul>
    {% else %}
        <p>No Blog articles are available.</p>
    {% endif %}

  - We created a django template using an HTML file
  - The Django templates give the user limited programming logic capabilities like variables, if-else & for loops

This is an if-else statement in Django templates
      
      {% if %}
        ...
      {% else %}
        ...
      {% endif %}
      
This is a for loop
      
      {% for var in my_list %}
        ...
      {% endfor %}
      
  - variables and objects are represented as {{ my_variable }}
  - *Narrator's note* We will discuss more about Django templating later in the series 

Demonstration [03:00 | 08:00]
----------------
Now let's modify the blog/views.py as follows
    
    from django.http import HttpResponse
    from django.shortcuts import render
    
    
    from .models import Blog, Article

    def index(request):
        article_list = Articles.objects.all()
        context = {'article_list': article_list}
        return render(request, 'blog/index.html', context)

  - Run the django server
  - Access the link localhost:8000/blogs/


*** With this we come to the end of the tutorial***
 ----------------------------------------------------
 *** Add concluding slides and assignment***[00:42 | 08:42]
 -------------------------------------------