如何在django中修复NoReverseMatch?

时间:2012-10-10 14:33:18

标签: python django templates view django-urls

当我在页面上运行我的django应用程序时出现以下错误

"partner/buzzfeed/": NoReverseMatch at /partner/BuzzFeed/ Reverse for 'video_player' with arguments '('BuzzFeed', 'fXkqhhIlOtA', 'NY Yankees: 6 Essential Pieces of Postseason Memorabilia')' and keyword arguments '{}' not found.

看起来正确的变量是从模板传递的,但是没有与url正确匹配(我认为)。关于如何解决它的任何建议?

views.py

def partner_channel(request,partner_name):
    file = urllib2.urlopen('http://gdata.youtube.com/feeds/api/users/'+partner_name+'/uploads?v=2&max-results=50')
    data = file.read()
    file.close()        
    root = etree.fromstring(data)       
    entries = root.findall('{http://www.w3.org/2005/Atom}entry')        
    feed=[]
    i=0 #for feed list tracking
    for entry in entries:
        #find media:group
        media = entry.find('{http://search.yahoo.com/mrss/}group')      
        #get description, title, thumbnail
        desc = media.find('{http://search.yahoo.com/mrss/}description').text
        title = media.find('{http://search.yahoo.com/mrss/}title').text
        thumbnail = media.find('{http://search.yahoo.com/mrss/}thumbnail[2]').attrib.get("url")
        video = media.find('{http://gdata.youtube.com/schemas/2007}videoid').text
        author = entry.find('{http://www.w3.org/2005/Atom}author').find('{http://www.w3.org/2005/Atom}name').text
        feed.append([i,title, desc, thumbnail, video, author, url])     
        i=i+1       
    return render_to_response('reserve/templates/partner.html', {'feed':feed},
        context_instance=RequestContext(request))

def video_player(request,author,video,title):   
    return render_to_response('reserve/templates/video_player.html', {'author':author, 'video':video, 'title':title},
        context_instance=RequestContext(request))

模板

{% for video in feed %}    
    <a href="{% url video_player feed.1.5 video.4 video.1   %}" >Link</a>
{% endfor %}

urls.py

urlpatterns = patterns('reserve.views',
    url(r'^partner/(?P<partner_name>[-\w]+)/$', 'partner_channel'),
    url(r'^partner/(?P<author>[-\w]+)/(?P<video>[-\w]+)/(?P<video_title>[-\w]+)/$', 'video_player'),
    url(r'^partner/(?P<author>[-\w]+)/(?P<video>\w+)/(?P<title>\w+)/$', 'video_player'),
)

1 个答案:

答案 0 :(得分:0)

你可以做以下两件事之一:

在模板中,更改

{% url video_player feed.1.5 video.4 video.1 %}

{% url reserve.views.video_player feed.1.5 video.4 video.1 %}

或者:

为网址添加名称:

url(r'^partner/(?P<author>[-\w]+)/(?P<video>[-\w]+)/(?P<video_title>[-\w]+)/$', 'video_player', name="video-player"),

并将模板调用更改为

{% url video-player feed.1.5 video.4 video.1 %}