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 %}
I'd made a few changes.