Django NoReverse Match Error

时间:2015-12-13 02:35:52

标签: python regex django web jinja2

我一直试图解决'NoReverse Match'几个小时。

错误:

NoReverseMatch at /myCourses/
Reverse for 'removeFromCurrentlyEnrolledList' with arguments '()' and keyword arguments '{'courseID': '15-122'}' not found. 1 pattern(s) tried: ['myCourses/removeFromCurrentlyEnrolledList/(?P<courseID>.....)$']
    Request Method: GET
    Request URL:    http://127.0.0.1:8000/myCourses/
    Django Version: 1.9
    Exception Type: NoReverseMatch
    Exception Value:    
    Reverse for 'removeFromCurrentlyEnrolledList' with arguments '()' and keyword arguments '{'courseID': '15-122'}' not found. 1 pattern(s) tried: ['myCourses/removeFromCurrentlyEnrolledList/(?P<courseID>.....)$']
    Exception Location: E:\00 CMU\15112\courseReview\myvenv\lib\site-packages\django\core\urlresolvers.py in _reverse_with_prefix, line 508
    Python Executable:  E:\00 CMU\15112\courseReview\myvenv\Scripts\python.exe
    Python Version: 3.4.0
    Python Path:    
    ['E:\\00    CMU\\15112\\courseReview',
     'C:\\Windows\\system32\\python34.zip',
     'D:\\01 SOFTWARES\\Python 3.4\\DLLs',
     'D:\\01 SOFTWARES\\Python 3.4\\lib',
     'D:\\01 SOFTWARES\\Python 3.4',
     'E:\\00    CMU\\15112\\courseReview\\myvenv',
     'E:\\00    CMU\\15112\\courseReview\\myvenv\\lib\\site-packages']
    Server time:    Sat, 12 Dec 2015 21:22:06 -0500

看起来它能够捕获传递给它的参数?但我不知道为什么它无法解决它。

我尝试了以下每种网址格式:

url(r'^myCourses/removeFromCurrentlyEnrolledList/(?P<courseID>.....)$', views.removeFromCurrentlyEnrolledList, name='removeFromCurrentlyEnrolledList'),
url(r'^myCourses/removeFromCurrentlyEnrolledList/(?P<courseID>/d+)$', views.removeFromCurrentlyEnrolledList, name='removeFromCurrentlyEnrolledList'),
url(r'^myCourses/removeFromCurrentlyEnrolledList/(?P<courseID>[0-9][0-9]-[0-9][0-9][0-9])$', views.removeFromCurrentlyEnrolledList, name='removeFromCurrentlyEnrolledList'),

相关的HTML +模板

<h6><big>Courses Enrolled</big></h6>
{% for item in currentList %}
     <p>{{ item }}</p> <a href="{% url 'removeFromCurrentlyEnrolledList' courseID=item %}">R</a>
{% endfor %}

相关观点

def removeFromCurrentlyEnrolledList(request, courseID):
    userProfile = getAssociatedUserProfile(request.user.id)
    userProfile.removeFromUserCurrentlyEnrolled(courseID)
    return redirect('courseDirectory.views.myCourses')

添加堆栈跟踪的屏幕截图,以确保我没有遗漏任何内容

Stack trace Screenshot

请帮忙。

谢谢。

1 个答案:

答案 0 :(得分:0)

删除所有三个urlconf并添加此项(使用\d+代替/d+):

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

我不确定item是什么,但它应该是ID item.id而不是对象:

{% for item in currentList %}
 <p>{{ item }}</p> 
 <a href="{% url 'removeFromCurrentlyEnrolledList' courseID=item.id %}">R</a>
{% endfor %}