带有单一争论的Django NoReverseMatch

时间:2016-01-03 02:30:34

标签: python django

我目前正试图从一个有一个争论的模板中调用网址。在尝试解决{%url' rep' object.person.id%}我得到一个带有以下文本的NoReverseMatch异常。

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

它似乎找到了正确的模式和争论是我所期待的,但由于某种原因它并没有匹配。有没有人看到任何跳出来的东西?我现在已经把头撞在墙上好几个小时了,我觉得这将是一个愚蠢的错误。

该应用的所有代码都可以在下面找到。

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 %}

2 个答案:

答案 0 :(得分:1)

<Route path="employees" component={Employees}>
    <Route path=":id/edit" component={EmployeesEdit} />
</Route>
<Route path="dashboard" component={Dashboard}/>

[u'replist/$(\\d+)/$', u'$(\\d+)/$'] 匹配字符串的结尾。在字符串结束后,你显然无法匹配任何内容。您需要在与$一起使用的正则表达式中删除项目的URLconf中的尾随$

答案 1 :(得分:0)

要纠正的事情:

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

应该是

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) )

应该是

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