django: link to detail page from list of returned results
<p>I'm creating a page that returns a list of movies with basic details after a user search. </p>

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

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

<p>the data is coming from <a href="https://www.themoviedb.org/documentation/api" rel="nofollow">TMDB's api</a> 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.</p>

<p>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.</p>

<p>forms.py:</p>

<pre><code>from django import forms


class MovieSearch(forms.Form):
    moviename = forms.CharField(label="Search", max_length=250)
</code></pre>

<p>views.py:</p>

<pre><code>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})
</code></pre>

<p>and html:</p>

<pre><code>{% extends 'moviecompare/compare.html' %}

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

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

<p>site's urls.py:</p>

<pre><code>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')),
</code></pre>

<p>and the app's urls.py:</p>

<pre><code>from django.conf.urls import url
from . import views

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

<p>edit: adding my 1st (failed) attempt at the movie detail view:</p>

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

<p>I was able to get this working by fixing the urlpattern per Roman's answer, along with a few needed tweaks.</p>

<p>in the app urls.py I needed to change the order:</p>

<pre><code>urlpatterns = [
    url(r'^(?P&lt;movid&gt;[0-9]+)$', views.get_movie, name='movie_detail'),
    url(r'^', views.search_movie, name='compare'),
]
</code></pre>

<p>and in the root urls.py I have:</p>

<pre><code>    url(r'^compare/', include('moviecompare.urls', namespace='compare')),
</code></pre>

<p>added a function to the view:</p>

<pre><code>def get_movie(request, movid):
    movie = tmdb.Movies(movid)
    response = movie.info()
    context = {
        'response': response
    }
    return render(request, './moviecompare/detail.html', context)
</code></pre>

<p>and used this to link the detail in the html:</p>

<pre><code>{% url 'compare:movie_detail' movid=key.id %}
</code></pre>

python html django