每当我试图打开我的博客文章时,我都会收到这个消息。我的博客网址是http://leonkong.com/ 发布url为http://leonkong.com/1/
错误消息:
NoReverseMatch at /1/
Reverse for 'like_action' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['(?P<pk>[0-9]+)\\/like_action\\/$']
我使用django版本2.0.4,最近使用awsrds创建了mysql。
仅供参考,如操作功能代码(views.py):
from django.contrib.auth.models import User
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.http import JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt
def like_action(request, pk):
"""Like action: add +1 to post.like_button"""
post = get_object_or_404(Post, pk=pk)
post.like_button = post.like_button + 1
post.save()
return redirect('post_detail', pk=post.pk)
URL.py:
from django.urls import path
from . import views
urlpatterns = [
path('keyboard', views.buttons, name='buttons'),
path('message', views.message, name='message'),
path('', views.post_list, name='post_list'),
path('<int:pk>/', views.post_detail, name='post_detail'),
path('post/new/', views.post_new, name='post_new'),
path('post/<int:pk>/edit/', views.post_edit, name='post_edit'),
path('about', views.about, name='about'),
path('post/<int:pk>/remove/', views.post_remove, name='post_remove'),
path('<str:category>', views.category_list, name='category_list'),
path('<int:pk>/like_action/', views.like_action, name='like_action'),
]
下面是“post\u detail”的html代码。每当我尝试访问“post\u detail”页面时,总会出错。
{% extends 'blog/base.html' %}
{% load markdownify %}
{% load static %}
{% block content %}
<div class="post">
{% if post.created_date %}
<div class="date">
{{ post.created_date }}
</div>
{% endif %}
<h1 class="detail">{{ post.title }}</h1>
<div class="edit">
{% if user.is_authenticated %}
<a href="{% url 'post_edit' pk=post.pk %}">edit</a>
<a class="delete" href="{% url 'post_remove' pk=post.pk %}">delete</a>
{% endif %}
</div>
<div class="content">
{{ post.text|markdown|safe }}
</div>
<div class="interaction">
<p style="color: #4e57ac; display: inline; font-weight: bold; font-family: sans-serif;">
{{ post.like_button }}
</p>
{% if post.like_button < 2 %}
<a href="{% url 'like_action' pk=post.pk %}" style="font-weight: bold; font-family: sans-serif;">Like</a>
{% else %}
<a href="{% url 'like_action' pk=post.pk %}" style="font-weight: bold; font-family: sans-serif;">Likes</a>
{% endif %}
<div class="fb-share-button" data-href="http://leonkong.com{% url 'post_detail' pk=post.pk %}" data-layout="button" data-size="large" data-mobile-iframe="true"><a target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fleonkong.com%2F&src=sdkpreparse" class="fb-xfbml-parse-ignore">Share</a></div>
</div>
</div>
{% endblock %}
结果如下:
> DESC blog_post;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | 0 | |
| title | varchar(200) | NO | | NULL | |
| text | longtext | NO | | NULL | |
| created_date | datetime(6) | YES | | NULL | |
| author_id | int(11) | NO | | NULL | |
| category | varchar(5) | NO | | NULL | |
| like_button | int(11) | NO | | NULL | |
+--------------+--------------+------+-----+---------+-------+
视图.post\u细节
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'posts': post})
暂无答案!
目前还没有任何答案,快来回答吧!