0

I am learning Django and I am wondering if there is a better way to do this. Basically I am trying to get movies from the IMDbPy API and display the movie information. As of now I am using one view that gets the information and also displays it (using two different templates). Is this the right way to do this? Or should I split this onto two different views? If so, how do I do this?

Views

def get_movie_name_view(request):
form = GetMovieName(request.POST or None)
if form.is_valid():
    ia = IMDb()
    movies = ia.get_movie(form.cleaned_data['movie_title'])
    context = {'title': movies['title'],
               'directors': movies['directors'],
               'runtime': movies['runtime'],
               'year': movies['year'],
               'genre': movies['genres'],
               'form': form
               }
    return render(request, 'show_movie_info.html',context)
context = {
    'form': form
}
return render(request, 'get_movie_name.html', context)

Model

class Movie(models.Model):
  title = models.CharField(max_length=250)
  directors = models.CharField(max_length=300)
  runtime = models.IntegerField()
  year = models.DateField()
  genre = models.CharField(max_length=100)

forms

class GetMovieName(forms.Form):
movie_title = forms.CharField(label='Movie Title', max_length=100)

Get Movie template

{%  extends 'base.html' %}

{% block content %}
    <form method="POST"> {% csrf_token %}
        {{form.as_p}}
    <input type="submit", value="Submit" />
    </form>
{%  endblock %}

Show movie template

{%  extends 'base.html' %}

{% block content %}
    <p> Title: {{ title }} </p>
    <p> Duration: {{ runtime }} minutes </p>
    <p> Director: {{ directors }} </p>
    <p> Year: {{ year }} </p>
    <p> Genre: {{ genre }}</p>


{%  endblock %}
David
  • 487
  • 2
  • 6
  • 18
  • Are you intentionally not saving the form? – Mint Aug 20 '18 at 21:23
  • Yes, I don't want the form to be saved (if possible). Only to display the results at first. Later on if the user wishes, he or she can save the movie information into their account. But at first my goal is only to display the information – David Aug 20 '18 at 21:30
  • Ok, understood. – Mint Aug 20 '18 at 21:33

1 Answers1

0

I'd made a few changes.

def get_movie_name_view(request):
    form = GetMovieName(request.POST or None)
    # ensure the request is a post
    if request.method == 'POST':
        if form.is_valid():
            ia = IMDb()
            movies = ia.get_movie(form.cleaned_data['movie_title'])
            context = {'title': movies['title'],
                       'directors': movies['directors'],
                       'runtime': movies['runtime'],
                       'year': movies['year'],
                       'genre': movies['genres'],
                       'form': form
                       }
            return render(request, 'show_movie_info.html',context)
    # ensure request is a GET
    if request.method == 'GET':
        context = { 'form': form }
        return render(request, 'get_movie_name.html', context)
Mint
  • 1,013
  • 1
  • 12
  • 29