渲染时捕获到NoReverseMatch

2024-10-02 08:25:43 发布

您现在位置:Python中文网/ 问答频道 /正文

呈现时捕获到NoReverseMatch:Reverse for'accounts.views.new\fn找不到带参数“()”和关键字参数“{student\u pk”:****}。 (星号实际上是数字,我只是删改了它们)。你知道吗

不管怎样,我不太明白为什么这不起作用。在谷歌这个错误,似乎这是由于没有正确定义网址,然而,我看不出这是怎么回事!像往常一样,谢谢你的帮助!你知道吗

从网址.py你知道吗

 (r'^lockers/(?P<course_pk>\w+)/$', 'lockers'),
 (r'^lockers/(?P<course_pk>\w+)/assignlocker/$', 'lockerassign'),

从视图.py你知道吗

 @user_is_valid
 def lockers(request, course_pk):
     print("Lockers - A")
     course = get_object_or_404(Course, pk=course_pk)
     students =       Student.objects.filter(--redacted, but this does work)
print("Render to Response - B")
return render_to_response("accounts/locker_roster.html", {'students':students, 'lockers':orderedlockers}, context_instance=RequestContext(request))


 @user_is_valid
 def lockerassign(request,student_pk):
if request.method == "POST":
    print("Method is Post - C")
    pass
else:
    print("Render form - D")
    lockers = Locker.objects.raw(sql query, which does in fact work)
    student = Student.object.get(pk=student_pk)
    print("Render to Response - E")
    return render_to_response("accounts/locker_assign.html",{'student':student , 'lockers':lockers} , context_instance=RequestContext(request))  

从储物柜_花名册.html你知道吗

 {%for student in students%}
             <tr>
                <td>{{student}}</td>
                <td style="text-align: right">
                  <a href="{% url accounts.views.lockerassign student_pk=student.pk%}">Assign Locker</a>
                 </td>
             </tr>
         {%endfor%}   

注意:googlechrome标记了学生迭代中的错误。你知道吗


Tags: toforisrequesthtmlrenderstudenttd
2条回答

在urlconf中为URL指定了一个name属性后,需要使用该名称而不是函数路径来反转它。你知道吗

{% url lockerassign student_pk=student.pk %}

(我从您最初没有使用引号的事实推测,您使用的是Django<;1.5。如果使用的是更高版本,则应将视图名称加引号。)

要使用{% url %}标记,您需要将name属性添加到您的url中,以便Django能够反转它们:

(r'^lockers/(?P<course_pk>\w+)/$', name='lockers'),
(r'^lockers/(?P<course_pk>\w+)/assignlocker/$', name='lockerassign'),

要测试是否正确地反转了它们,请使用:^{}。你知道吗

相关问题 更多 >

    热门问题