我的班级基础视图有问题,我无法解决!我尝试渲染一个显示详细帖子的网页。但是当我尝试路由http://127.0.0.1:8000/post/1/时,我得到了this 当我尝试获取http://127.0.0.1:8000/时,效果很好。
我完全不明白这一点!
我的 urls.py
from django.urls import path
from .views import PostListView, PostDetailView
from .models import Post
from . import views
urlpatterns = [
path('', PostListView.as_view(), name='blog-home'),
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
path('about/', views.about, name='blog-about'),
]
我的 view.py
# pylint:disable=no-member
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post
def home(request):
context = Post.objects.all()
return render(request, {'posts': context})
class PostListView(ListView):
model = Post
context_object_name = 'posts'
ordering = ['-date_posted']
class PostDetailView(DetailView):
model = Post
def about(request):
return render(request, 'blog/about.html', {'title': 'About'})
我的 post_detail.html
{% extends "blog/base.html" %}
{% block content %}
<article class="media content-section">
<div class="media-body">
<div class="article-metadata">
<img class="img-profile" src="{{ object.author.profile.image.url }}" />
<a class="mr-2" href="#">{{ object.author }}</a>
<small class="text-muted">{{ object.date_posted | date:"d F, Y " }}</small>
<hr />
</div>
<h2 class="article-title">{{ object.title }}</h2>
<p class="article-content">{{ object.content }}</p>
</div>
</article>
{% endblock content %}
谢谢:)
答案 0 :(得分:0)
根据我的分析,我认为您是在写post_detail.py
而不是post_detail.html
这是.html
文件,但我误将其写入.py
。这就是为什么它将尝试查找post_datail.html的原因,但是它将找不到该文件并抛出404 page not found error
答案 1 :(得分:0)