反向使用关键字参数 '{'pk': 1}' 未找到 'likes'尝试了 1 个模式:['datasecurity/likes/<int:pk>'] 像 django 中的按钮错误

问题描述

注册登录页面正常工作,但我的按钮不起作用..我不知道为什么...... 有人能帮我解决这个问题吗... 这将是很大的帮助 请帮忙 谢谢!

  1. views.py`

     from django.shortcuts import render,get_object_or_404
     from datasecurity.models import Post
     from django.urls import reverse
     from django.http import HttpResponseRedirect
     from django.contrib.auth.decorators import login required
    
    
     # Create your views here.
     def datasecurity(request):
          allPosts= Post.objects.all()
          context={'allPosts': allPosts}
           return render(request,'datasecurity/data.html',context=context)
    
     def blogHome(request,slug):
          post=Post.objects.filter(slug=slug).first()
          context={"post":post}
          return render(request,"datasecurity/blogHome.html",context)
    
     @login_required
     def likes(request,pk):
          post=get_object_or_404(Post,id=request.POST.get('post_id'))
          post.likes.add(request.user)
          return HttpResponseRedirect(reverse('datasecurity:blogHome',args=str(pk)))
    
  2. urls.py

    from django.conf.urls import url
    
    from . import views
    
    app_name = 'datasecurity'
    
    urlpatterns = [
            url(r'^$',views.datasecurity,name="datasecurity"),url(r'^datasecurity/(?P<slug>[^/]+)',views.blogHome,name='blogHome'),url(r'^likes/<int:pk>',views.likes,name = "likes"),]
    
  3. data.html

     {% extends 'careforallapp/navbar.html' %}
     {% block body_block %}
      {% load static %}
    

    欢迎使用数据安全

        {% for post in allPosts  %}
    
         <div class="line-dec"></div>
         <span
           >This is a Bootstrap v4.2.1 CSS Template for you. Edit and use
           this layout for your site. Updated on 21 May 2019 for repeated main menu HTML code.</span
         >
       </div>
       <div class="left-image-post">
         <div class="row">
           <div class="col-md-6">
             <div class="left-image">
               {% if post.img %}
                 <img src="{{ post.img.url }}" alt="" />
               {% endif %}
             </div>
           </div>
           <div class="col-md-6">
             <div class="right-text">
               <h4>{{post.title}}</h4>
               <h6>Article  by {{post.author}}</h6>
               <h2>{{post.datetime}}</h2>
    
               <p>
                 {{post.content|safe | truncatechars:280}}
               </p>
    
               <from action = "{% url 'datasecurity:likes' pk=post.pk %}" method = "POST">
                 {% csrf_token %}
                 <button type="submit" name="post_id" value = "{{ post_id }}" class="btn"> Like 
               </button>
               </form>
    
               <div class="white-button">
                 <a href="{% url 'datasecurity:blogHome' slug=post.slug %}">Read More</a>
               </div><br>
             </div>
                {% endfor %}
    
  4. 错误信息 <from action = "{% url 'datasecurity:likes' pk=post.pk %}" method = "POST">

  • 未找到关键字参数为 '{'pk': 1}' 的 'likes' 反向。尝试了 1 个模式:['datasecurity/likes/int:pk']

在调试代码时突出显示了此消息。所以有人可以告诉我我在代码中遗漏了什么...... 谢谢!

解决方法

您没有从视图中获取 pk 参数。将您的视图更改为:

@login_required
 def likes(request,pk):
      post=get_object_or_404(Post,id=pk)

网址

url(r'^likes/(?P<pk>\d+)/',views.likes,name = "likes"),

注意 url 中使用的正则表达式。