diff options
-rw-r--r-- | tutorial_7_creating_blog_list_interface/slides.md | 59 |
1 files changed, 56 insertions, 3 deletions
diff --git a/tutorial_7_creating_blog_list_interface/slides.md b/tutorial_7_creating_blog_list_interface/slides.md index 0b8dc9e..561a57c 100644 --- a/tutorial_7_creating_blog_list_interface/slides.md +++ b/tutorial_7_creating_blog_list_interface/slides.md @@ -39,6 +39,59 @@ Slide 5: - Modify the blog/views.py and add a new view to display the Blog List of a particular user - def user_blogs(request, user): - - + def get_blogs(request, user): + user_object = User.object.get(username=user) + blogs = Blog.objects.filter(creator=user_object) + context = {'blogs': blogs} + return render(request, 'myapp/index.html', context) + +Slide 6: +--------------------- + +**Modify the blogs template** + +- Modify the template created previously, located at ```/blog/templates/blog/blogs.html``` to look like below + + # /blog/templates/blog/blogs.html + {% if blogs %} + <ul> + {% for blog in blogs %} + <li>{{ blog.name}}</li> + <ul> + {% for article in blog.article_set %} + <li>{{ article.title }}</li> + {% endfor %} + </ul> + {% endfor %} + </ul> + {% else %} + <p>No Blogs are available.</p> + {% endif %} + +Slide 7: +--------------------- + +** Modify the URLs ** + +- Modify the urls located in the ```myproject``` folder + + # /myproject/urls.py + from django.conf.urls import include, url + from django.contrib import admin + from blog import urls + + urlpatterns = [ + url(r'^admin/', admin.site.urls), + url(r'^blog/$', blog.urls) # Add this line + ] + +- Modify the urls located in the ```blogs``` folder + + # /blogs/urls.py + from django.conf.urls import patterns, url + from blog import urls + + urlpatterns = [ + url(r'^(?P<username>\w+)/list/$', views.get_blogs, name='blogs') # Add this line + ] + |