diff options
author | King | 2017-06-21 15:35:50 +0530 |
---|---|---|
committer | ankitjavalkar | 2017-06-22 16:56:02 +0530 |
commit | e9ba1b41b1164e0f30c16b07dd366c9ed9d817bb (patch) | |
tree | a326961247f278646869aa49a08e889b9d93b365 | |
parent | 6f09d472ebfc1e8015bf614054973e2af7059ea8 (diff) | |
download | learn_django-e9ba1b41b1164e0f30c16b07dd366c9ed9d817bb.tar.gz learn_django-e9ba1b41b1164e0f30c16b07dd366c9ed9d817bb.tar.bz2 learn_django-e9ba1b41b1164e0f30c16b07dd366c9ed9d817bb.zip |
Split tutorial4 in two tutorials
The fourth one is now only views and urls.
Fifth will be about templates.
Also changed the flow, now its step by step explanation of views.
-rw-r--r-- | tutorial_4_django_views_templates/slides.md | 127 |
1 files changed, 46 insertions, 81 deletions
diff --git a/tutorial_4_django_views_templates/slides.md b/tutorial_4_django_views_templates/slides.md index 40fa44d..4ecf773 100644 --- a/tutorial_4_django_views_templates/slides.md +++ b/tutorial_4_django_views_templates/slides.md @@ -1,6 +1,6 @@ -Tutorial: Create Views and Route your URLS +Tutorial: Create Views and Route your URLS(urls setting) =========================================== -[Demonstration time: 10 mins 50 s (0.85 ~ 85%) | Total time: 12 mins 42 s] +[Demonstration time: mins s (0.85 ~ 85%) | Total time: mins s] Slide 1 [00:08 | 00:08] ------------ @@ -14,7 +14,6 @@ Slide 2 [00:12 | 00:20] In this tutorial, we will learn to; - Create a django view - - Create a django template - Create a url routing scheme Slide 3 [00:11 | 00:31] @@ -40,7 +39,7 @@ Slide 5 [00:10 | 00:52] - A view is code that accepts a request - It processes the request and sends back a response -Demonstration [03:00 | 03:52] +Demonstration [02:00 | 02:52] ----------- **Creating a View** @@ -49,19 +48,16 @@ Edit the /blog/views.py # /blog/views.py from django.http import HttpResponse - from .models import Blog, Article + from .models import Blog def get_blogs(request): blogs = Blog.objects.all() # This is called a query - for blog in blogs: - articles = Article.objects.filter(blog=blog) - response = 'Blog: {0}\n\n'.format(b) + '\n'.join([article.title for article in articles]) - return HttpResponse(response) + return HttpResponse(blogs) - Narrator Notes: Please state that Django queries will be explained later in the series, and should explain the above code. -Demonstration [03:50 | 07:42] +Demonstration [02:50 | 05:42] ----------- **Add URL routing to URLConf** @@ -72,10 +68,11 @@ This is called the URL Dispatcher # /myproject/urls.py from django.conf.urls import include, url from django.contrib import admin + from blog import views urlpatterns = [ url(r'^admin/', admin.site.urls), - url(r'^$', views.index, name='index') # Add this line + url(r'^blogs/$', views.get_blogs, name='blogs') # Add this line ] - Run the django server using command: @@ -83,82 +80,50 @@ This is called the URL Dispatcher - **Narrator Note**: Show the web browser to the user - Go to the url http://localhost:8000/blogs/ and show the output. - - You will see the article that you had added in the tutorial 3 + - You will see the blog object Query set. + - So we have created a simple client-server model.(At this point we can show + the image we showed in previous tutorial.) + - Let us now improve our view. + + +Demonstration [01:30 | 06:22] +----------- +In the /blog/views.py edit get_blogs function - Slide 6 [00:18 | 08: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 [03:00 | 11: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 [01:00 | 12: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 get_blogs(request): + blogs = Blog.objects.all() # This is called a query + response = 'Blogs:\n\n' + for blog in blogs: + response += '{0}\n'.format(blog) + return HttpResponse(response) + + - Narrator Notes: Should explain the above code. + - Save and again show the browser. + - We now see the individual blog object created in tutorial 3. + + +Demonstration [01:30 | 07:52] +----------- +Let us further edit the view to display articles related to a blog. +In the /blog/views.py edit get_blogs function - def index(request): - article_list = Articles.objects.all() - context = {'article_list': article_list} - return render(request, 'blog/index.html', context) + + def get_blogs(request): + blogs = Blog.objects.all() # This is called a query + response = 'Blogs:\n\n' + for blog in blogs: + articles = Articles.objects.filter(blog=blog) + response += '{0}\n'.format(blog) + response += '\n'.join([article.title for article in articles]) + return HttpResponse(response) - - Run the django server - - Access the link localhost:8000/blogs/ + - Narrator Notes: Should explain the above code. + - Save and again show the browser. + - We now also see the article created in the tutorial 3. *** With this we come to the end of the tutorial*** ---------------------------------------------------- - *** Add concluding slides and assignment***[00:42 | 12:42 ] +*** Add concluding slides and assignment***[00:42 | 08:34] ------------------------------------------- - |