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 %}

<ul>

<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>

</ul>

{% 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 = 'https://ghibliapi.herokuapp.com/films/'
        
        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()
        print(results)

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

        movies.append(movie_data)  
        print(movie_data) 
     

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

This is my html:

{% load static %}
<!DOCTYPE html>
<html>
<head>
    <title>Ghibli Studio | Movies</title>
    <link rel="stylesheet" href="{% static 'core/main.css' %}">
</head>
    <body>
      <div class=" header">
         
      </div>
      <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="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
                  <button type="submit" class="searchButton">

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

              </div>


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


      </div>
</body>
</html>

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 %}
    <ul>
        <li>Director: {{ movie.director }}</li>
        <li>Title: {{movie.title}}</li>
        <li>Year: {{movie.release_date}}</li>
        <li>Description: {{movie.description}}</li>
    </ul>
      {% 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