Slide 1 ------------ Title Slide **Creating an Interface to view the Blog** Slide 2 -------------- **Learning Objectives** In this tutorial, we will learn to; - Create a new view with custom queries - Create an interface to view all blogs and articles 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 models in django - how to write django queries - how to create templates - how to write views - If not, see the relevant django tutorial on http://spoken-tutorial.org Slide 5: ---------------- **Create a new View** - Modify the blog/views.py, modify the get_blogs view to display the Blog List of a particular user in a template 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, 'blog/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 {% if blogs %}
No Blogs are available.
{% 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) # Change 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'^(?PCreated: {{ article.created_on }}
{{ article.body}}
{% else %}No Articles are available for this blog.
{% endif %} Slide 10b: ------------------- **Modify the blog template** - We need to add a link to each article in the blog list page ```/blog/templates/blog/blogs.html``` so that a user can view an articles {% if blogs %}No Blogs are available.
{% endif %} Slide 11 ------------------------- **Add URLs for articles_list** - We need to add a new URL in ```/blogs/urls.py``` which will point to the articles_list view. - When selecting a particular blog the URL should look like ```localhost:8000/blog/