【问题标题】:Django NoReverse Match ErrorDjango NoReverse 匹配错误
【发布时间】:2015-12-13 02:35:51
【问题描述】:

我已经尝试解决“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 模式:

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

请帮忙。

谢谢。

【问题讨论】:

    标签: python regex django web jinja2


    【解决方案1】:

    删除所有三个 urlconfs 并添加它(使用 \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 %}
    

    【讨论】:

    • 这不起作用...它能够捕获参数“courseID”(屏幕截图中的“15-122”)。但是在尝试 1 种模式后模式匹配失败。这很奇怪......谢谢你的帮助。
    猜你喜欢
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2014-10-03
    • 1970-01-01
    • 2014-01-20
    • 2018-10-20
    相关资源
    最近更新 更多