User0511
User0511

Reputation: 725

Django regular expression error NoReverseMatch

I am getting error use django regular expression like this:

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]+)/$']

I want to call data with "id_fleet_id". this is my example data:

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))

template

<td><a href="{% url 'fleet_member_edit' id_fleet_id=post.id_fleet_id %}" type="button" class="btn btn-primary">EDIT</a></td>

can you help me solve this problem?

Upvotes: 0

Views: 64

Answers (1)

Rahul Gupta
Rahul Gupta

Reputation: 47846

You are not sending the post object in the template context.

You need to send post object in the context as @cdvv7788 also mentioned. Since, you are not sending the post object in the context, the template is not able to generate the url properly.

You need to do something like:

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) 

Upvotes: 1

Related Questions