Я новичок в 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>
Как я могу решить эту проблему, пожалуйста?