django - NoReverseMatch 在 /post/31/
问题描述
我是 django 的新手,并创建了一个博客,在使用基于类的视图时,我的帖子详细信息显示正确。我决定使用基于函数的视图,我想出了这个错误。请帮忙。
这是我的views.py 文件
# class PostDetailView(DetailView):
# model = Post
# template_name = 'ecommerce/post_detail.html'
# def get_context_data(self, *arg, **kwargs):
# context = super().get_context_data(**kwargs)
# form = CommentForm()
# context['form'] = form # Your comment form
# return context
def post_detail(request, pk):
post = get_object_or_404(Post, pk = pk)
is_liked = False
if post.likes.filter(id=request.user.id).exists():
is_liked = True
context ={
'post':post,
'is_liked':is_liked,
}
return render(request, 'ecommerce/post_detail.html', context)
这是我的完整 urls.py
from django.urls import re_path
from django.conf.urls import include, url
from .views import (
PostListView,
#PostDetailView,
#PostCreateView,
post_create_view,
post_detail,
PostUpdateView,
PostDeleteView,
UserPostListView
)
from .import views
urlpatterns = [
path('', PostListView.as_view(), name='ecommerce-home'),
path('user/<str:username>', UserPostListView.as_view(), name='user-posts'),
re_path(r'^post/(?P<pk>[0-9]*)/$', views.post_detail, name="post-detail"),
#path('post/<int:pk>/', views.post_detail, name='post-detail'),
#url(r'^post/(?P<slug>[-\w]+)-(?P<pk>\d+)/$', views.post_detail, name='post-detail'),
#path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
#path('post/new/', PostCreateView.as_view(), name='post-create'),
path('post/new/', post_create_view, name='post-create'),
path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
path('about/', views.about, name='ecommerce-about'),
path('commonuser/', views.commonuser, name='ecommerce-commonuser'),
path('like/$/', views.like_post, name='like_post'),
]
我的 post_detail.html
{% extends "ecommerce/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<article class="media content-section">
<img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="{% url 'user-posts' object.author.username %}">{{ object.author }}</a>
<small class="text-muted">{{ object.date_posted|date:"F d, Y"}}</small>
{% if object.author == user %}
<div>
<a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'post-update' object.id %}">Update</a>
<a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'post-delete' object.id %}">Delete</a>
</div>
{% endif %}
</div>
<h2 class="article-title">{{ object.title }}</h2>
<p class="article-content">{{ object.content }}</p>
</div>
<form action="{% url 'like_post' %}" method="POST">
{% csrf_token %}
{% if is_liked %}
<button type="submit" name="post_id" value="{{ post.id }}" class="btn btn-danger">Dislike</button>
{% else %}
<button type="submit" name="post_id" value="{{ post.id }}" class="btn btn-primary">Like</button>
{% endif %}
</form>
请问我该如何解决这个问题?
解决方案
推荐阅读
- javascript - 我如何创建一个程序来循环播放 p5.js 中的视频
- environment-variables - 如何在 nuxt 项目中强制 .env 变量更新?
- javascript - 如何为胜利图设置多背景颜色
- python - 从张量1打开表文件到张量流2 python3
- java - 如何使这个 char 替换工作?
- javascript - javascript在字符串中捕获css规则块
- javascript - 从 api 推送数组数据会导致无限循环
- python - 如何在特定范围内选择一个空闲端口
- ruby-on-rails - Rails 自加入迁移
- c - 如何绘制区域选择框