Django正则表达式错误NoReverseMatch

时间:2015-09-16 03:42:26

标签: python regex django

我收到错误使用django正则表达式,如下所示:

  

Reverse for 'fleet_member_edit' with arguments '()' and keyword arguments '{u'id_fleet_id': ''}' not found. 1 pattern(s) tried: ['dashboard/fleet_member_edit/(?P<id_fleet_id>[0-9]+)/$']

我想用&#34; id_fleet_id&#34;来调用数据。这是我的示例数据:

id id_fleet_id id_unit 
1   2323        A1 
2   2323        A2 
3   2343        A8

urls.py

url(r'^dashboard/fleet_member_edit/(?P<id_fleet_id>[0-9]+)/$', 'apps.fleet.views.fleet_member_edit', name='fleet_member_edit'),

views.py

def fleet_member_edit(request, id_fleet_id):
    post = get_object_or_404(Member, id_fleet_id=id_fleet_id)
    if request.method == "POST":
        id_fleet = request.POST['id_fleet'] 
        units = map(int, request.POST['id_unit'].split(), instance=post)

        for id_unit in units:
            member = Member()
            member.id_fleet_id = id_fleet
            member.id_unit_id = id_unit
            member.save()
        return redirect('apps.setup.views.vehicle_group_view')
    else:
        form = FleetMember(instance=post)
    context_dict = {'form':form, 'post':post}
    return render(request,'fleet_member_edit.html', context_dict, context_instance= RequestContext(request))

模板

<td><a href="{% url 'fleet_member_edit' id_fleet_id=post.id_fleet_id %}" type="button" class="btn btn-primary">EDIT</a></td>
你可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您没有在模板上下文中发送post对象。

您需要在上下文中发送post对象,同时提及@cdvv7788。由于您没有在上下文中发送post对象,因此模板无法正确生成网址。

您需要执行以下操作:

def my_view(request):
    ...
    context_dict = {'post': post, ..} # pass the 'post' object
    # here 'some_template.html' is the template containing this url
    return render(request, 'some_template.html', context_dict)