我正在尝试添加到change_friend网址和视图的链接,但我得到一个反向匹配,因为我显然没有传递正确的参数。
这里是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')
]
这是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')
这是我在(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 %}
对我来说,看起来我正在传递正确的论据。这是我得到的错误:
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>/']
任何帮助都会非常感激
答案 0 :(得分:2)
您正在使用旧路径语法和旧网址方法。将其更改为路径:
path('connect/<slug:operation>/<int:pk>/', ...