在基于类的视图上找不到页面(404)

时间:2020-02-23 15:29:29

标签: python django

我的班级基础视图有问题,我无法解决!我尝试渲染一个显示详细帖子的网页。但是当我尝试路由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 %}

谢谢:)

2 个答案:

答案 0 :(得分:0)

根据我的分析,我认为您是在写post_detail.py而不是post_detail.html

这是.html文件,但我误将其写入.py。这就是为什么它将尝试查找post_datail.html的原因,但是它将找不到该文件并抛出404 page not found error

答案 1 :(得分:0)

那斯敏那是一个愚蠢的错误!!

我只是在http://127.0.0.1:8000/post/4/上发出了一个请求,这是可行的;

所以post / 1-3就是不要退出。

对不起,我很愚蠢