summaryrefslogtreecommitdiff
path: root/tutorial_9_create_login/slides.md
blob: 0f72561dabd5c79a356fe8a759b704c35a46c3a7 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
Slide 1
------------
Title Slide
**Django Authentication**

Slide 2
--------------

**Learning Objectives**

In this tutorial, we will learn to;
  - Create login functionality
  - Using django built-in login and logout functions

Slide 3
---------------

**System Requirements**
  - Ubuntu 16.10
  - Python 3.5 or higher version
  - python3.4-venv

Slide 4:
----------------

**Introduction**

- Let us use our knowledge of forms and templates to create a login system for our blog application
- We will use Django's built-in authentication system for this.

Demonstration
------------------

**Add a new form**

- We will now add a login form to the ```forms.py``` file located in ```blog``` folder
- Add the following code to the file;

      from django.contrib.auth.forms import AuthenticationForm 
      from django import forms

      class LoginForm(AuthenticationForm):
          username = forms.CharField(label="Username", max_length=30, 
                        widget=forms.TextInput(attrs={'name': 'username'}))
          password = forms.CharField(label="Password", max_length=30, 
                        widget=forms.PasswordInput(attrs={'name': 'password'}))

**explain this in short, any complex concept then can mention will be covered in the upcoming tutorials**

Demonstration
------------------

**Create a new template**

- Create a template ```login.html``` at ```/blog/templates/blog/login.html``` to look like below

        <html>
        <body>
          <form method="post">
              {% csrf_token %}
              {{ form.as_p }}
              <input type="submit" value="login" />
          </form>
        </body>
        </html>

**explain this in short, any complex concept then can mention will be covered in the upcoming tutorials**

Demonstration
----------------

**Modify the urls.py**

- Modify the urls.py file located in the ```myproject``` folder


      from django.contrib import admin
      from django.urls import path, include
      from django.contrib.auth import views as auth_views # Add this import

      urlpatterns = [
          path('admin/', admin.site.urls),
          path('blogs/', include('blog.urls', namespace='blog')),
          path('login/', auth_views.login, {'template_name': 'blog/login.html',
               'authentication_form': LoginForm}, name='login'), # Add this line
      ]

**explain this**

Demonstration
--------------------

**Modify settings.py**

- Add the following:

        LOGIN_REDIRECT_URL = '/blogs/get_blogs/'

**explain this**

Demonstration
--------------------

**Modifying the views**

- You will have to add this line ```@login_required(login_url="/login/")``` above all the function in the ```views.py```
- This is called a decorator.
- It is a special function and a built-in feature in django that allows you to verify if the current session of the User is authenticated.
- In case the user is not logged in / authenticated, the user is redirected to the link specified in the variable ```login_url```
- Note: There is a setting for LOGIN_URL available(You may use this instead!)

Example:

      # blog/views.py

      @login_required(login_url="/login/")
      def get_blogs(request):
          ...


At this point, run the server and show login demo using the super user credentials.
You can visit page ```http://localhost:8000/blogs/get_blogs/``` and show the demo


Demonstration
---------------------

**Add logout url**

- Modify the ```urls.py``` located in 

      # myproject/urls.py

      from django.contrib import admin
      from django.urls import path, include
      from django.contrib.auth import views as auth_views # Add this import
      from blog.forms import LoginForm

      urlpatterns = [
        path('admin/', admin.site.urls),
        path('blogs/', include('blog.urls', namespace='blog')),
        path('login/', auth_views.login, {'template_name': 'blog/login.html',
             'authentication_form': LoginForm}, name='login'), # Add this line
        path('logout/', auth_views.logout, {'next_page': '/login'},
             name='logout'), # Add this line
      ]


Demonstration
---------------------

- Add a link to the ```/logout``` url to the template blogs.html as following:

      <p><a href="{% url 'logout' %}">Logout</a></p>

At this point, run the server and show logout demo, as well as login and logout demo again in different ways.
For example:
1. We can show http://localhost:8000/blogs/get_blogs/ and it reditrects to login page if user is not authenticated, else we see the blogs
2. We can show http://localhost:8000/login/ and demonstrate login and redirects!

**Concluding and Assignment Slides**