bhildebrand
bhildebrand

Reputation: 11

Django NoReverseMatch with single argument

I'm currently trying to call a URL from a template that has a single argument. When attempting to resolve {% url 'rep' object.person.id %} I get a NoReverseMatch exception with the following text.

Reverse for 'rep' with arguments '(400034,)' and keyword arguments '{}' not found. 2 pattern(s) tried: [u'replist/$(\\d+)/$', u'$(\\d+)/$']

It seems like it's finding the correct pattern and the argument is what I'm expecting, but it just isn't matching up for some reason. Does anyone see anything that jumps out at them? I've been banging my head against the wall for hours now, and I feel like it's going to be a stupid mistake.

All the code for the app can be found below.

urls.py:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.replist, name='main'),
    url(r'^(\d+)/$', views.rep, name='rep'),
]

views.py:

from django.shortcuts import render_to_response, render

import time
import urllib2
import json
import unicodedata

def replist(request):
    poli_link = "https://www.govtrack.us/api/v2/role?current=true"

    req = urllib2.Request(poli_link)
    response = urllib2.urlopen(req)
    html = response.read()

    reps = json.loads(html)

    return render_to_response("replist/rep_list.html", dict(reps=reps))

def rep(request, repid ):
    return render_to_response("replist/rep.html", dict(rep=rep) )

rep_list.html:

{% extends "replist/bbase.html" %}

{% load taglookup %}

{% block content %}
    <style type="text/css">
        .main { margin-left: 25px; margin-right: 25px; float: left; width: 75%; margin-top: 30px; }
        .sidebar { float: left; margin-top: 30px; }
        .time { font-size: 0.8em; margin-top: 2px; }
        .body { font-size: 1.1em; margin-top: 2px; }
        .commentlink { text-align: right; }
        .step-links a { font-size: 0.89em; }
        .title {
            font-size: 1.4em; margin-top: 20px; border-bottom: 1px solid #ccc;
            padding-left: 4px; margin-left: 5px;
        }
        .messages { margin-left: 20px; }
        .pagination { margin-top: 20px; margin-left: -20px; }
    </style>
    <div class="main">
    {% for object in reps|get_item:"objects" %}
    <a href="{% url 'rep' object.person.id %}">{{object.person.name}}</a><br>
    {% endfor %}
    </div>
{% endblock %}

Upvotes: 1

Views: 27

Answers (2)

doniyor
doniyor

Reputation: 37886

things to correct:

url(r'^(\d+)/$', views.rep, name='rep'),

should be

url(r'^(?P<repid>\d+)/$', views.rep, name='rep'),

views.py

def rep(request, repid ):
    return render_to_response("replist/rep.html", dict(rep=rep) )

should be

def rep(request, repid):
    # get rep from db and render to template
    return render(request, "replist/rep.html", {'rep': rep})

Upvotes: 0

knbk
knbk

Reputation: 53679

[u'replist/$(\\d+)/$', u'$(\\d+)/$']

$ matches the end of a string. You obviously cannot match anything after the end of the string. You need to remove the trailing $ in your project's URLconf, in the regexes used with include().

Upvotes: 1

Related Questions