diff options
author | ankitjavalkar | 2017-07-26 16:41:37 +0530 |
---|---|---|
committer | GitHub | 2017-07-26 16:41:37 +0530 |
commit | c5a4e6de19a0a93586f04344698d94c298225386 (patch) | |
tree | 786843c82b132ad722277b4a9718c3e9b147f136 /tutorial_9_create_login | |
parent | fa14c2b19896000fce61d64d073e7ab549f8548d (diff) | |
download | learn_django-c5a4e6de19a0a93586f04344698d94c298225386.tar.gz learn_django-c5a4e6de19a0a93586f04344698d94c298225386.tar.bz2 learn_django-c5a4e6de19a0a93586f04344698d94c298225386.zip |
Add more slides
Diffstat (limited to 'tutorial_9_create_login')
-rw-r--r-- | tutorial_9_create_login/slides.md | 91 |
1 files changed, 78 insertions, 13 deletions
diff --git a/tutorial_9_create_login/slides.md b/tutorial_9_create_login/slides.md index 9564d9f..9f55466 100644 --- a/tutorial_9_create_login/slides.md +++ b/tutorial_9_create_login/slides.md @@ -35,25 +35,23 @@ Slide 5: - Modify the urls.py file located in the ```myproject``` folder - # /myproject/urls.py - from django.conf.urls import include, url - from django.contrib import admin - from blog import views - from django.contrib.auth import views as auth_views # Add this import + from django.conf.urls import include, url + from django.contrib import admin + from blog import views + from django.contrib.auth import views as auth_views # Add this import - urlpatterns = [ - url(r'^admin/', admin.site.urls), - url(r'^blogs/$', include('blogs.urls')), - url(r'^login/$', auth_views.login, {'template_name': 'login.html'}), # Add this line - ] + + urlpatterns = [ + url(r'^admin/', admin.site.urls), + url(r'^blogs/$', include('blogs.urls')), + url(r'^login/$', auth_views.login, {'template_name': 'login.html'}), # Add this line + ] Slide 6: ------------------ -**Create a new template** - -Create a template login.html at /blog/templates/blog/login.html to look like below +**Create a new template* - Create a template ```login.html``` at ```/blog/templates/blog/login.html``` to look like below @@ -83,3 +81,70 @@ Create a template login.html at /blog/templates/blog/login.html to look like bel </body> </html> +Slide 7: +------------------ + +**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 + + # If you don't do this you cannot use Bootstrap CSS + 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.TextInput(attrs={'name': 'password'})) + + +Slide 8: +-------------------- + +**Add the form to the URL configuration** + +- We will now add the form that we just created to the URL configuration of ```/login``` URL + + # myproject/urls.py + + from django.conf.urls import include, url + from django.contrib import admin + from blog import views + from django.contrib.auth import views as auth_views + from blog.forms import LoginForm + + urlpatterns = [ + url(r'^admin/', admin.site.urls), + url(r'^blogs/$', include('blogs.urls')), + url(r'^login/$', auth_views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}}), # Add this variable 'authentication_form' + ] + +Slide 8: +-------------------- + +**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``` + +Example: + + # blog/views.py + + @login_required(login_url="login/") + def get_blogs(request, username): + ... + + @login_required(login_url="login/") + def edit_blogs(request, blog_id): + ... + + @login_required(login_url="login/") + def edit_articles(request, article_id): + ` ... + + |