Arsalan Shahid

Creating Django Views and Design Templates – MovieStore Part 2

My dear reader, how are you? السلام عليكم

Perseverance is a great element of success. If you only knock long enough and loud enough at the gate, you are sure to wake up somebody – Henry Wadsworth Longfellow

This post is a continuation of  Django MovieStore series. We will extend our previous tutorial and start with the description of ListView. We will also explore how to set URLs in Django and make the views interact with templates.


Before I start, I would like to provide you with a link to the GitHub Repo that contains all the codes (MovieStore GitHub Repo => DirectMe). You can clone it on to your local machine and experiment at your own as shown below:

$ git clone https://github.com/ArsalanShahid116/Django-MovieStore.git
$ cd Django-MovieStore
Django-MovieStore$ source myenv/bin/activate
(myenv)Django-MovieStore$ pip3 install -r requirements.txt # install the required packages

Looking for a link to MovieStore part 1? DirectMe


I now start by first explaining ListView. ListView requires at least one attribute called model. It will query for all the rows of that model, pass it to the template, and return the rendered template in a response.

Adding the first template: Django comes with its own template language called the Django Template language. But, Django can also be integrated with other template languages such as Genshi, Jinja2 and Mako. Most Django users find the default template language to be more efficient and convenient. In the default configuration that is generated in our settings.py file. Each Django application can have a templates directory.

We will now make our first template. First, create a list.html in the templates directory as shown below:

(myenv)Django-MovieStore/MyMovies/moviesApp/templates/moviesApp/$ touch index.html
# add the following code in index.html
<!DOCTYPE html>
<html>
    <body>
      <ul>
          {% for myMovie in object_list %}
             <li> {{ myMovie }} </li>
          {% empty %}
             <li> No movies yet </li>
          {% endfor %}
      </ul>
    </body>
</html>

Django templates are simple HTML with the variables (say, object_list as in our example) and tags (for example, for in our above template). Variables will be evaluated to strings by being surrounded with {{   }}.


Adding routes to view using URLConf: We will now need to add URL routes to our templates. Create a urls.py file in moviesApp and add the following code.

(myenv)Django-MovieStore/MyMovies/moviesApp$ touch urls.py

# add the following code to urls.py

from django.urls import path
from . import views
app_name = 'moviesApp'
urlpatterns = [
    path('myMovies',
    views.MovieList.as_view(),
    name='MovieList'),
]

# After this add the following lines of code to movies/url.py

from django.contrib import admin
from django.urls import path, include
import moviesApp.urls # add this
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include(moviesApp.urls, namespace='moviesApp')), #add this
]

After this, run the server and go to http://127.0.0.1:8000/myMovies in your browser to see the list of movies.

Django also supports detail view of individual movie pages in our MovieStore.


In the next post, we will continue to extend our MovieStore tutorial, create detailed views and associate the movie models to users. Add a user view, and define a userManager function.


I hope you find this post useful. If you find any errors or feel any need for improvement, let me know in your comments below.

Have you worked with Django? Let me know your experience in comments.

Signing off for today. See you next week! Happy learning.

Exit mobile version