Django template doesn't show model items
<p>I'm very new to Django and I'm currently trying to create a movie database for a few of my favorite directors. I've been following tutorial videos, and I'm currently trying to get my detail view to display all the added movies for each director. However when I go into the director, it does not display their films.</p>

<p>director/models.py</p>

<pre><code>from django.db import models

class Director(models.Model):
    photo = models.CharField(max_length=250, default='none')
    name = models.CharField(max_length=250)
    born = models.CharField(max_length=250)
    birth_date = models.CharField(max_length=20)
    married = models.CharField(max_length=1)

def __str__(self):
    return self.name

class Films(models.Model):
    director = models.ForeignKey(Director, on_delete=models.CASCADE)
    title = models.CharField(max_length=50)
    year = models.IntegerField(default=0)
    rating = models.DecimalField(max_digits=2, decimal_places=1, default=0)
    budget = models.CharField(max_length=20)

    def __str__(self):
        return self.title
</code></pre>

<p>director/urls.py</p>

<pre><code>from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),

    url(r'^(?P&lt;name_id&gt;[0-9]+)/$', views.detail, name='detail'),

]  
</code></pre>

<p>director/views.py</p>

<pre><code>from django.shortcuts import render
from django.http import HttpResponse
from .models import Director, Films

def index(request):
    all_directors = Director.objects.all()
    return render(request, 'director/index.html', {'all_directors':   all_directors})


def detail(request, name_id):
    try:
        direct = Director.objects.get(pk=name_id)
    except Director.DoesNotExist:
    raise Http404("Director not found")
    return render(request, 'director/detail.html', {'direct': direct})
</code></pre>

<p>director/detail.html</p>

<pre><code>&lt;h3 align="center"&gt; {{ direct }} &lt;/h3&gt;&lt;br&gt;

&lt;img src="{{ director.photo }}"&gt;

&lt;h4&gt;Film List&lt;/h4&gt;

&lt;ul&gt;
    {% for films in director.films_set.all %}
    &lt;li&gt;{{ direct.title }} - {{ direct.rating }}&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>When I run the server and go to the page, I click on a director and all it says is the directors name, and "Film List". Please advise on how to fix this, any help is greatly appreciated!</p>

<p>In your view, you only pass one context object to the template, <code>direct</code>. This <code>direct</code> is a member of your director model: it makes sense that <code>direct</code> in your template displays the name of the director, since that is how you define the <code>__str__</code> method for that model.</p>

<p>Beyond that, though, none of your other variables will display. Why?</p>

<ul>
<li>You use <code>director.photo</code> instead of <code>direct.photo</code>. <code>director</code> is undefined in the template. (As a side note, <code>director</code> is IMO a better variable name.)</li>
<li>You use <code>direct.rating</code> and <code>direct.title</code> even though these are attributes of the <code>Film</code> model. You will need to modify as follows:</li>
</ul>

<pre>
    &lt;ul>
        {% for film in direct.films_set.all %}
        &lt;li>{{ film.title }} - {{ film.rating }}&lt;/li>
        {% endfor %}
    &lt;/ul>
</pre>

<p>(I also changed <code>films</code> to <code>film</code> since that more accurately reflects the variable's content.)</p>

python django