How to display search results in html with django

Hello, I am writing a search app that returns a movie from ghibli studio once you search for the title. The app it’s connected to the ghibli API and it works because whenever I type the title in the search bar, I get the result back but Only in my terminal. I need to display the results in the html but what I’ve tried so far has not worked.
I’ve been told to do this in my html file:

{% if movie %}

{% for movie in movies %}


<li>Title: {{ movie\_data.title }}</li>

<li>Release date: {{ movie\_data.release\_date }}</li>

<li>Director: {{ movie\_data.director }}</li>

<li>Description: {{movie\_data.description}}</li>


{% endfor %}

{% endif %}

This doesn’t work. This is my view:

from django.shortcuts import render, HttpResponse
from core.models import Movie
from django.views.generic import ListView
import requests

def index(request):
    movies = []
    if request.method == 'POST':
        film_url = ''
        search_params = {
            'films' : 'title',
            'films' : 'description',
            'films' : 'director',
            'films' : 'release_date',
            'q' : request.POST['search']

        r = requests.get(film_url, params=search_params)
        results = r.json()

        for result in results:
             movie_data = {
                'Title' : result['title'],
                'Release_date': result['release_date'],
                'Director' : result['director'],
                'Producer' : result['producer'],
                'Description' : result['description']


    context = {
    'movies' : movies
    return render(request,'core/index.html', context)

This is my html:

{% load static %}
<!DOCTYPE html>
    <title>Ghibli Studio | Movies</title>
    <link rel="stylesheet" href="{% static 'core/main.css' %}">
      <div class=" header">
      <div class="wrap">
          <form action='/' method="POST">
              {% csrf_token %}
              <div class="search">

                  <input type="text" name="search" class="searchTerm" placeholder="Type movie name" >
                  <link rel="stylesheet" href="">
                  <button type="submit" class="searchButton">

                      <i class="fa fa-search" style="font-size:24px"></i>


          {% if movie %}
           {% for movie in movies %}
              <li>{{ movie }}</li>
           {% endfor %}
          {% endif %}


Any help would be very much appreciated!

What’s the

block for? Your context doesn’t seem to contain a movie variable, so that might already be the reason that the enclosed for block isn’t rendering. It seems like

{% if movies %}

would make more sense. :wink:

1 Like

Whoa, it was that easy! It worked! Thank you so much :smile:

1 Like

Would you happen to know how I can display the result in the same way it appears in my movie_data dictionary?
I’m trying this but it displays nothing if I do this, whereas it does display the whole block if I just do the {{movie}} thing, but it’s hard to understand since everything is together:

{% if movie %}
 {% for movie in movies %}
        <li>Director: {{ movie.director }}</li>
        <li>Title: {{movie.title}}</li>
        <li>Year: {{movie.release_date}}</li>
        <li>Description: {{movie.description}}</li>
      {% endfor %}
    {% endif %}

That should work in principle, try capitalizing the same way as in the movie_data dict (e.g. {{ movie.Title }}).

1 Like

Thank you so much again! That solved it! :smile:

1 Like