Nader
Nader

Reputation: 77

No reverse Match error: cannot find the slug or the primary key I'm passing in

I am trying to add links to the change_friend url and view, but I am getting a reverse match because Im apparently not passing in the right arguments.

here is friends.urls.py:

from . import views
from django.conf.urls import url



app_name = 'friends'

urlpatterns = [
    # we have 1 url for both adding and losing a friend
    url('connect/<slug:operation>/<int:pk>/', views.change_friends, name='change_friends'),
    # url(r'^connect/(?P<operation>.+)/(?P<pk>\d+)/$', views.change_friends, name='change_friends')
]

here is the friends.views.py:

from django.shortcuts import render, redirect
from friends.models import Friend
from django.contrib.auth.models import User




def change_friends(request, operation, pk):
    friend = User.objects.get(pk=pk)
    if operation == 'add':
        Friend.make_friend(request.user, friend)
    elif operation == 'lose':
        Friend.lose_friend(request.user, friend)
    return redirect('groups:index')

here is the template i'm calling it in (profile.html):

  {% if user in friends %}
          <a href="{% url 'friends:change_friends' 'remove' user_profile.id %}"><button type="button" name="btn btn-warning">UnFriend</button></a>
        {% else %}
          <a href="{% url 'friends:change_friends' 'add' user_profile.id %}"><button type="button" name="btn btn-success">Befriend</button></a>
        {% endif %}

to me, it looks like i'm passing in the right arguments. this is the error im getting:

NoReverseMatch at /accounts/profile/4
Reverse for 'change_friends' with arguments '('remove', 4)' not found. 1 pattern(s) tried: ['friends\\/connect/<slug:operation>/<int:pk>/']

any help would be really appreciated

Upvotes: 0

Views: 123

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599956

You're using the new path syntax with the old url method. Change it to path:

path('connect/<slug:operation>/<int:pk>/', ...

Upvotes: 2

Related Questions