【问题标题】:Page not found (404) add comment找不到页面 (404) 添加评论
【发布时间】:2020-10-16 14:46:08
【问题描述】:

大家好,我正在尝试对我的 django 项目添加评论,但出现 404 错误,我不知道这是为什么 这是我的代码:

urls.py

    re_path(r'(?P<slug>[-\w]+)/' , views.details , name="details"),
    path('comment/<prId>)' , views.comment , name='comment'),

views.py

def details(request , slug):
    pr = get_object_or_404(Product , slug=slug)
    category = Category.objects.all()
    id1 = pr.category.id
    similar = Product.objects.filter(category_id=id1)
    pr.view = F('view')+1
    pr.save()
    context = {
        'products': pr,
        'similar' : similar,
        'category' : category
    }
    return render(request, "prDetails.html" , context)

def comment(request, prId):
    pr = get_object_or_404(Product ,id=prId)
    pr2 = pr.slug

    cm = Comment(
        name=request.POST.get('name', ''),
        email=request.POST.get('email' , ''),
        text=request.POST.get('text', ''),
        products=pr
    )
    cm.save()
    return HttpResponseRedirect(reverse('details', args=(pr2,)))


这是我的错误

Page not found (404)
Request Method: POST
Request URL:    http://127.0.0.1:8000/comment/1/
Raised by:  main.views.details
No Product matches the given query.

【问题讨论】:

    标签: python django django-views django-urls


    【解决方案1】:
    def comment(request, prId):
        pr = get_object_or_404(Product ,id=prId)
    

    您是否在数据库中创建了任何评论?

    【讨论】:

    • 是的,我发现问题是因为 re_path
    猜你喜欢
    • 2018-10-10
    • 1970-01-01
    • 2021-02-25
    • 2015-10-30
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 2020-04-28
    相关资源
    最近更新 更多