Python 就像巴顿一样,我不断得到:相反的';比如';没有找到任何参数。尝试了1种模式:[产品/(?P<;slug>;[-a-zA-Z0-9]+;)$&]

Python 就像巴顿一样,我不断得到:相反的';比如';没有找到任何参数。尝试了1种模式:[产品/(?P<;slug>;[-a-zA-Z0-9]+;)$&],python,json,django,Python,Json,Django,我用JSON代码在Django中创建了一个like按钮,但我一直得到“NoReverseMatch”。在url.py和models.py中,我显示了所有的代码,在views.py中,仅显示like按钮和导入的方法,在detail.html中,仅显示like按钮和JSON脚本的html标记。 这就是我在浏览器中看到的: Reverse for 'like' with no arguments not found. 1 pattern(s) tried: ['products/(?P<slug

我用JSON代码在Django中创建了一个like按钮,但我一直得到“NoReverseMatch”。在url.py和models.py中,我显示了所有的代码,在views.py中,仅显示like按钮和导入的方法,在detail.html中,仅显示like按钮和JSON脚本的html标记。 这就是我在浏览器中看到的:

Reverse for 'like' with no arguments not found. 1 pattern(s) tried: ['products/(?P<slug>[-a-zA-Z0-9_]+)$']
    Request Method: GET
    Request URL:    http://localhost:8000/products/7/
    Django Version: 2.2.9
    Exception Type: NoReverseMatch
    Exception Value:    
    Reverse for 'like' with no arguments not found. 1 pattern(s) tried: ['products/(?P<slug>[-a-zA-Z0-9_]+)$']

Python Version: 3.7.4
应用程序“产品”中的views.py:

产品应用程序中的URL.py:

from django.urls import path, include
from . import views

urlpatterns = [
    path('create', views.create, name = 'create'),
    path('<int:product_id>/', views.detail, name = 'detail'),
    path('<int:product_id>/upvote', views.upvote, name = 'upvote'),
    path('user/<int:fk>', views.hunterhunts, name = 'hunterhunts'),
    path('<slug:slug>', views.like, name='like'),
]
从django.url导入路径,包括
从…起导入视图
URL模式=[
路径('create',views.create,name='create'),
路径(“/”,views.detail,name='detail'),
路径('/upvote',views.upvote,name='upvote'),
路径('user/',views.hunterhunts,name='hunterhunts'),
路径(“”,views.like,name='like'),
]
“产品”应用程序中的HTML文件detail.HTML:


$('#like')。单击(函数(){
$.ajax({
类型:“POST”,
url:“{%url'类似“%}”,
数据:{
'slug':$(this.attr('name'),
“csrfmiddlewaretoken”:“{csrf_令牌}”
},
数据类型:“json”,
成功:功能(响应){
警报(response.message);
警报(‘产品喜欢次数现在是’+响应。喜欢次数);
},
错误:函数(rs,e){
警报(rs.responseText);
}
});
});
对于类似的按钮工作,是否知道应该更正或添加什么


如果可以,请明确说明

您需要将
slug
作为
url中的参数传递:“{%url'像“%”,


像这样:
url:“{%url'Like'product.slug%}”,

是的,这真的很有帮助,它显示了页面,但现在当单击“Like”按钮时,它会在数据库中传递它,因为“userX”喜欢“productX”,而计数也不起作用,知道为什么吗?
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from .models import Product
from django.contrib.auth.models import User
from django.utils import timezone
from django.views.decorators.http import require_POST
from django.http import HttpResponse
try:
    from django.utils import simplejson as json
except ImportError:
    import json

  .
  .
  .
@login_required(login_url = '/accounts/signup')
@require_POST
def like(request, slug):
    if request.method == 'POST':
        hunter = request.user
        #slug = request.POST.get('slug', None)
        product = get_object_or_404(Product, slug = slug)

        if product.likes.filter(hunter_id = hunter.id).exists():
            product.likes.remove(hunter)
            message = 'You disliked this'
            product.save()
        else:
            product.likes.add(hunter)
            message = 'You liked this'
            product.save()

    ctx = {'likes_count': product.total_likes, 'message': message}
    return HttpResponse(json.dumps(ctx), content_type='application/json')
from django.urls import path, include
from . import views

urlpatterns = [
    path('create', views.create, name = 'create'),
    path('<int:product_id>/', views.detail, name = 'detail'),
    path('<int:product_id>/upvote', views.upvote, name = 'upvote'),
    path('user/<int:fk>', views.hunterhunts, name = 'hunterhunts'),
    path('<slug:slug>', views.like, name='like'),
]
<input type="button" id="like" name="{{ product.slug }}" value="Like" />
                    <script>
                    $('#like').click(function() {
                      $.ajax({
                        type: "POST",
                        url: "{% url 'like' %}",
                        data: {
                          'slug': $(this).attr('name'),
                          'csrfmiddlewaretoken': '{{ csrf_token }}'
                        },
                        dataType: "json",
                        success: function(response) {
                          alert(response.message);
                          alert('Product likes count is now ' + response.likes_count);
                        },
                        error: function(rs, e) {
                          alert(rs.responseText);
                        }
                      });
                    });
                  </script>