Arsalan Shahid

Django Detail Views and Adding Model Relationships – MovieStore Part 3

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

Consistency is the foundation of virtue – Francis Bacon

This post is a continuation of Django tutorial series using an example MovieStore. I will explain how to add detailed views in our application. Further, we will define person relationships with our movie models and explain different types of relationships that may occur between different personality types such as movie writers, directors, actors and etc.

Before we start, a few useful links are as below:

  1. MovieStore GitHub Repository – (DirectMe)
    Use the following command to clone the directory.
    $ git clone
  2. How to Define Django Models and Create Views – MovieStore Part 1 (DirectMe).
    To fetch the GitHub project until this point, use the following command inside the cloned directory.
    Django-MovieStore$ git fetch origin fc62df78bb491b6c54ba261cb682a82da9cfc94a
  3. Creating Django Views and Design Templates – MovieStore Part 2 (DirectMe)
    Fetch to this point using key: d3ea7112782236cf6c2844115cd202ce9f53f186

First of all, we will create a Django Detail view. Open in moviesApp folder and add the following code:

from django.views.generic import (ListView, DetailView)
from moviesApp.models import myMovie

class MovieList(ListView):
    model = myMovie

class MovieDetail(DetailView):
    model = myMovie

Now as we added our view so we need to create a template. In order to create a template, let us now change the location for our templates directory to a commonplace which can be used by other Django applications as well. In simple, we will create a general templates directory to add a base template for our MovieStore which can be extended and used further in other templates.

open movies/, and edit the following line shown in bold in templates.

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'),],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [

Create a templates directory in Django project MyMovies as follows and add a base.html as shown below

(myenv)Django-MovieStore/MyMovies$ mkdir templates
(myenv)Django-MovieStore/MyMovies$ touch templates/base.html

# open base.html and add the following code into it
<!DOCTYPE html>
<html lang="en" >
    <head >
    <meta charset="UTF-8" >
    content="width=device-width, initial-scale=1, shrink-to-fit=no"
    <link href="" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" rel="stylesheet" crossorigin="anonymous" >
    <title >
    {% block title %}MyMovies{% endblock %}
             .mymdb-masthead {
              background-color: #EEEEEE;
              margin-bottom: 1em;
    </head >

        <div class="mymdb-masthead">
        <div class="container">
        <nav class="nav">
        <div class="navbar-brand">MyMDB</div>
        href="{% url 'moviesApp:MovieList' %}"

        <div class="container">
        <div class="row">
        <div class="col-sm-8 mymdb-main">
        {% block main %}{% endblock %}
        <div class="col-sm-3 offset-sm-1 mymdb-sidebar">
               {% block sidebar %}{% endblock %}
    </body >
</html >

Once done, we now add a detail view template that extends the base template as follows.

# create a new template 
(myenv)Django-MovieStore/MyMovies$touch moviesApp/templates/moviesApp/mymovie_detail.html
# add the following code into it

{% extends 'base.html' %}

{% block title %}
{{ object.title }} - {{ block.super }}
{% endblock %}

{% block main %}
<h1> {{ object }} </h1>
<p class="lead">
{{ object.plot }}
{% endblock %}

{% block sidebar %}
This is a side bar and can be used for movie rattings or other features
{% endblock %}

# {% extends 'base.html' %} If a template wants to extend another template the first line must be an extends tag. Django will look for the base template (which can extend another template) and execute it first, then replace the blocks.

we now update the list view using the base template as well by updating the mymovie_list.html template.

(myenv)Django-MovieStore/MyMovies$ vi moviesApp/templates/moviesApp/mymovie_list.html
# ----------------------------------
{% extends 'base.html' %}

{% block title %}
All Available Movies
{% endblock %}

{% block main %}
    {% for myMovie in object_list %}
    <li> <a href="{% url 'moviesApp:MovieDetail' %}">
    {{ myMovie }} </a> </li>
        {% empty %}
    <li> No movies yet </li>
    {% endfor %}
{% endblock %}

We will now add relationships between two models. We will first create a new model and name it as Person model and then add a relationship between Person and myMovie model using a ForeignKey.

# open moviesApp/ and add the following
class Person(models.Model):
    first_name = models.CharField(
    last_name = models.CharField(
    born = models.DateField()

    class Meta:
        ordering = (
            'last_name', 'first_name')

    def __str__(self):
        return '{}, {} ({})'.format(

We now add a new director variable into our myMovie model class that has a many-to-one relationship with Person class. Which means that a director can direct many movies but NOT the vice-versa.

# Extend the myMovie model class with the following in bold
class myMovie(models.Model):
    title = models.CharField(max_length=140)
    plot = models.TextField()
    year = models.PositiveIntegerField()
    director = models.ForeignKey(

We further add a new role for writers to myMovie class using a many-to-many relationship with Person class as follows

# Extend the myMovie model class with the following in bold
class myMovie(models.Model):

    writers = models.ManyToManyField(

Apart from this Django also supports one-to-many and one-to-one relationships.

In the next post, we will continue to extend our MovieStore tutorial by adding myMovie and Person custom managers and creating a user login/registration portal.

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

Signing off for today. Stay tuned and I will see you next week! Happy learning.


Exit mobile version