Jon Jon - 3 months ago 12
HTML Question

django: link to detail page from list of returned results

I'm creating a page that returns a list of movies with basic details after a user search.

After the search, I'd like the user to be able to click on a movie, and get more details about it.

here's a link to the site: (be gentle, I only started learning this stuff 2 months ago! hah) http://moniblog.pythonanywhere.com/compare/

the data is coming from TMDB's api and the initial "generic" search JSON response doesn't have specific details that I'd display on the movie's detail page, but it has an ID that will be used for the specific movie's search.

I'm only using views.py to grab/display the search results, and I'm not sure if this is the right way to go, or if I should use a model, but that's probably a different question.

forms.py:

from django import forms


class MovieSearch(forms.Form):
moviename = forms.CharField(label="Search", max_length=250)


views.py:

from django.shortcuts import render, get_object_or_404
from django.conf import settings

from .forms import MovieSearch
import tmdbsimple as tmdb

tmdb.API_KEY = settings.TMDB_API_KEY


def search_movie(request):
parsed_data = {'results': []}
if request.method == 'POST':
form = MovieSearch(request.POST)
if form.is_valid():
search = tmdb.Search()
query = form.cleaned_data['moviename']
response = search.movie(query=query)
for movie in response['results']:
parsed_data['results'].append(
{
'title': movie['title'],
'id': movie['id'],
'poster_path': movie['poster_path'],
'release_date': movie['release_date'][:-6],
'popularity': movie['popularity'],
'overview': movie['overview']
}
)
for i in range(2, 5 + 1):
response = search.movie(query=query, page=i)
for movie in response['results']:
parsed_data['results'].append(
{
'title': movie['title'],
'id': movie['id'],
'poster_path': movie['poster_path'],
'release_date': movie['release_date'][:-6],
'popularity': movie['popularity'],
'overview': movie['overview']
}
)
context = {
"form": form,
"parsed_data": parsed_data
}
return render(request, './moviecompare/movies.html', context)
else:
form = MovieSearch()
else:
form = MovieSearch()

return render(request, './moviecompare/compare.html', {"form": form})


and html:

{% extends 'moviecompare/compare.html' %}

{% block movies_returned %}
<div class="wrap">
<div class="compare-gallery">
{% for key in parsed_data.results|dictsortreversed:'release_date' %}
{% if key.poster_path and key.release_date and key.title and key.overview %}
<div class="gallery-item">
<img src="http://image.tmdb.org/t/p/w185/{{ key.poster_path }}">
<div class="gallery-text">
<div class="gallery-date"><h5><span><i class="material-icons">date_range</i></span> {{ key.release_date }}</h5></div>
<div class="gallery-title"><h3><a href="../detail/{{ key.id }}">{{ key.title }}</a></h3></div>
<div class="gallery-overview">{{ key.overview|truncatechars:80 }}</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
{% endblock %}


I've started playing with the urls.py to get something to work, but no luck so far.

site's urls.py:

urlpatterns = [
url(r'^$', home, name="home"),
url(r'^blog/', include('blog.urls', namespace='blog')),
url(r'^compare/', include('moviecompare.urls', namespace='compare')),
url(r'^movies/', include('moviecompare.urls', namespace='movies')),


and the app's urls.py:

from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^', views.search_movie, name='compare'),
url(r'^(?P<movid>[0-9])+$', views.get_movie, name='movies')
]


edit: adding my 1st (failed) attempt at the movie detail view:

def get_movie(request, movid=None):
instance = get_object_or_404(request, movid=movid)
context = {
'instance': instance
}
return render(request, './moviecompare/detail.html', context)

Answer

I think you should try by fixing the urls.py in this line:

url(r'^(?P<movid>[0-9])+$', views.get_movie, name='movies')

Move the "+" inside the brackets like that:

url(r'^(?P<movid>[0-9]+)$', views.get_movie, name='movies')