找不到Django中的NoReverseMatch:.... l'的反向,参数为(('',)'。尝试了1种模式:

时间:2018-07-30 02:45:00

标签: django

Django 2.0 python 3.7 win7

urls.py

urlpatterns += [    
    url(r'^myFWs/', views.LoanedFWsByUserListView.as_view(), name='my- 
    applied'),
    url(r'^myFWs/(?P<pk>[0-9]+)$', views.FWDetailView.as_view(), name='FW-detail'),
]

views.py

from .models import FW

class LoanedFWsByUserListView(LoginRequiredMixin,generic.ListView):


   model = FW
   paginate_by = 10
   template_name = 'catalog/FWinstance_list_applied_user.html'
   def get(self, request):
       form = FWForm()
       FW_list = FW.objects.all()
       return render_to_response(self.template_name, locals())

FWinstance_list_applied_user.html:

{% block content %}
        <h1>Applied FWs</h1>
        <p>FW_list:</p>{{FW_list}}
        {% if FW_list %}
        <ul>
        {% for FWinst in FW_list %}
        <li class="{% if FWinst.is_approved %}text-danger{% endif %}">-->
        <a href="{% url 'FW-detail' FWinst.FW.pk %}">{{FWinst.ODM_name}}</a> ({{ 
        FWinst.project_name }})
        </li>
        {% endfor %}
        </ul>
        {% else %}
        <p>Nothing.</p>
        {% endif %}       
        {% endblock %}

错误是:

NoReverseMatch at /catalog/myFWs/
Reverse for 'FW-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['catalog/myFWs/(?P<pk>[0-9]+)$']

1 个答案:

答案 0 :(得分:1)

很显然FWinst.FW.pk返回一个空字符串,并且您的正则表达式无法匹配。只需更改以下标签:

{% url 'FW-detail' FWinst.FW.pk %}

{% url 'FW-detail' FWinst.pk %}