Django error: NoReverseMatch
<p>I'm using Django 1.10 and python 3.4</p>

<p>The precise error is</p>

<pre><code>NoReverseMatch at /movies/movie/Twilight/
Reverse for 'movie-details' with arguments '(8,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['movies/movie/(?P&lt;movie_id&gt;\\d+)|(?P&lt;movie_name&gt;[a-zA-Z\\ ]+)/$']
</code></pre>

<p>The error is caused by this line: <code>{% url 'moviesrating:movie-details' movie.id %}</code> in template <em>moviesrating/select_movie.html</em></p>

<p>In file <em>moviesrating/urls.py</em>, which is correctly included in the main urls file, there are those lines:</p>

<pre><code>app_name = 'moviesrating'

urlpatterns = [
    url(r'^movie/(?P&lt;movie_id&gt;\d+)|(?P&lt;movie_name&gt;[a-zA-Z\ ]+)/$', view_movie, name = 'movie-details'),
]
</code></pre>

<p>which refers to function <em>view_movie</em> in <em>moviesrating/views.py</em>:</p>

<pre><code>def view_movie(request, movie_id, movie_name):
    if movie_id:
        movie = get_object_or_404(Movie, pk = movie_id)
    elif movie_name:
        try:
            movie = get_object_or_404(Movie, name = movie_name)
        except MultipleObjectsReturned: # There are two movies named 'Twilight'
            movies = get_list_or_404(Movie, name = movie_name)
            return render(request, "moviesrating/select_movie.html", {'movies': movies})
    else:
        movie = None
    return render(request, "moviesrating/movie.html", {'movie': movie})
</code></pre>

<p>The purpose of the url <em>/movies/movie/...</em> is to show a movie found by name or by id, the specific url pattern comes from this need.</p>

<p>The point is that the error shows that <strong>even if it doesn't find the reverse match it finds the right url pattern</strong> so I thought the pattern didn't match. Then I tried to change the line to:</p>

<pre><code>{% url 'moviesrating:movie-details' movie.id %}
{% url 'moviesrating:movie-details' movie_id=movie.id movie_name=None %}
{% url 'moviesrating:movie-details' movie.id None %}
{% url 'moviesrating.views.view_movie' movie.id %}
{% url 'moviesrating.views.view_movie' movie_id=movie.id %}
{% url 'moviesrating.views.view_movie' movie_id=movie.id movie_name=None %}
</code></pre>

<p>but none of them worked.</p>

<p>Viewing the link in the browser (ex. <em>http://localhost:8081/movies/movie/8</em>) works, so it's not that page that causes the problem.</p>

<p>At this point really I can't understand the problem, I've followed the passages explained in the <a href="https://docs.djangoproject.com/en/1.10/intro/tutorial03/#s-removing-hardcoded-urls-in-templates" rel="nofollow">django docs</a> and read a lot of questions already asked but none of them solved my problem.</p>

<p>If something is unclear or more information is needed please let me know and I will edit the post. Thanks</p>

<p>Here is the full stacktrace of the error:</p>

<pre><code>Environment:


Request Method: GET
Request URL: http://localhost:8081/movies/movie/Twilight/

Django Version: 1.10.1
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'todolist.apps.TodolistConfig',
 'moviesrating.apps.MoviesratingConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template C:\Users\fra\Programmazione\Python\myserver\moviesrating\templates\moviesrating\select_movie.html, error at line 12
   Reverse for 'movie-details' with arguments '(8,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['movies/movie/(?P&lt;movie_id&gt;\\d+)|(?P&lt;movie_name&gt;[a-zA-Z\\ ]+)/$']   2 : &lt;html&gt;
   3 : &lt;head&gt;
   4 : &lt;meta charset="ISO-8859-1"&gt;
   5 : &lt;title&gt;Choice your movie&lt;/title&gt;
   6 : &lt;/head&gt;
   7 : &lt;body&gt;
   8 : &lt;h2&gt;Choice of which movie named {{ movies.0.name }} would you see the details&lt;/h2&gt;
   9 : 
   10 : &lt;ul&gt;
   11 : {% for movie in movies %}
   12 :     &lt;li&gt; {% url 'moviesrating:movie-details' movie.id %} &lt;/li&gt;
   13 :     &lt;li&gt;&lt;a href="{{ movie_url }}"&gt;{{ movie.name }} del {{ movie.year }} diretto da {{ movie.director }}&lt;/a&gt;&lt;/li&gt;
   14 : {% endfor %}
   15 : &lt;/ul&gt;
   16 : &lt;/body&gt;
   17 : &lt;/html&gt;
   18 : 

Traceback:

File "C:\Users\fra\Programmazione\Python\myserver\moviesrating\views.py" in view_movie
  31.             movie = get_object_or_404(Movie, name = movie_name)

File "C:\Program Files (x86)\Python\lib\site-packages\django\shortcuts.py" in get_object_or_404
  85.         return queryset.get(*args, **kwargs)

File "C:\Program Files (x86)\Python\lib\site-packages\django\db\models\query.py" in get
  389.             (self.model._meta.object_name, num)

During handling of the above exception (get() returned more than one Movie -- it returned 2!), another exception occurred:

File "C:\Program Files (x86)\Python\lib\site-packages\django\core\handlers\exception.py" in inner
  39.             response = get_response(request)

File "C:\Program Files (x86)\Python\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Program Files (x86)\Python\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\fra\Programmazione\Python\myserver\moviesrating\views.py" in view_movie
  34.             return render(request, "moviesrating/select_movie.html", {'movies': movies})

File "C:\Program Files (x86)\Python\lib\site-packages\django\shortcuts.py" in render
  30.     content = loader.render_to_string(template_name, context, request, using=using)

File "C:\Program Files (x86)\Python\lib\site-packages\django\template\loader.py" in render_to_string
  68.     return template.render(context, request)

File "C:\Program Files (x86)\Python\lib\site-packages\django\template\backends\django.py" in render
  66.             return self.template.render(context)

File "C:\Program Files (x86)\Python\lib\site-packages\django\template\base.py" in render
  208.                     return self._render(context)

File "C:\Program Files (x86)\Python\lib\site-packages\django\template\base.py" in _render
  199.         return self.nodelist.render(context)

File "C:\Program Files (x86)\Python\lib\site-packages\django\template\base.py" in render
  994.                 bit = node.render_annotated(context)

File "C:\Program Files (x86)\Python\lib\site-packages\django\template\base.py" in render_annotated
  961.             return self.render(context)

File "C:\Program Files (x86)\Python\lib\site-packages\django\template\defaulttags.py" in render
  209.                     nodelist.append(node.render_annotated(context))

File "C:\Program Files (x86)\Python\lib\site-packages\django\template\base.py" in render_annotated
  961.             return self.render(context)

File "C:\Program Files (x86)\Python\lib\site-packages\django\template\defaulttags.py" in render
  439.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "C:\Program Files (x86)\Python\lib\site-packages\django\urls\base.py" in reverse
  91.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "C:\Program Files (x86)\Python\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
  392.             (lookup_view_s, args, kwargs, len(patterns), patterns)

Exception Type: NoReverseMatch at /movies/movie/Twilight/
Exception Value: Reverse for 'movie-details' with arguments '(8,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['movies/movie/(?P&lt;movie_id&gt;\\d+)|(?P&lt;movie_name&gt;[a-zA-Z\\ ]+)/$']
</code></pre>

<p>You are only sending one parameter to your view, though it expects two. If you want to stick with one that can be interpreted as an id or a name, why not accept an alpha-numeric parameter </p>

<pre><code>urlpatterns = [
url(r'^movie/(?P&lt;movie_id&gt;[A-z0-9]+)/$', view_movie, name = 'movie-details'),
]
</code></pre>

<p>and then check in your view which one it is so you know if you should treat it as an id or a name</p>

<pre><code>def view_movie(request, movie_id):
     try: 
         int(movie_id)
         movie_name = False
    except:
         movie_name = True

    if not movie_name:
        movie = get_object_or_404(Movie, pk = movie_id)
    elif movie_name:

    # etc.
</code></pre>

python django