简体   繁体   English

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

[英]Page not found (404) on a Class-based views

I have a problem with my class bases views and i can't resolve it!我的类基础视图有问题,我无法解决! i try to render a web page that show a detailed post.我尝试呈现一个显示详细帖子的网页。 But when i try to route http://127.0.0.1:8000/post/1/ i get this While when i try get http://127.0.0.1:8000/ is work perfectly fine.但是当我尝试路由http://127.0.0.1:8000/post/1/ 时我得到了这个而当我尝试获取http://127.0.0.1:8000/ 时工作得很好。

I dont completely don't understand this!我不完全不明白这一点!

my urls.py我的网址.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'),
]

my view.py我的观点.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'})

my post_detail.html我的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 %}

thanks:)谢谢:)

I think as per my analysis you are writing post_detail.py instead of post_detail.html我认为根据我的分析,您正在编写post_detail.py而不是post_detail.html

This is .html file but my mistake you wrote it .py .这是.html文件,但我的错误是你把它写成了.py That's why it will try to find post_datail.html but it will can't able to find this file and throw 404 page not found error这就是为什么它会尝试找到 post_datail.html 但它无法找到这个文件并抛出404 page not found error

Nethermin it was a stupid mistakes!! Nethermin这是一个愚蠢的错误!!

I just make a request on http://127.0.0.1:8000/post/4/ and it's work;我只是在http://127.0.0.1:8000/post/4/上提出请求,它的工作;

So post/1-3 just dont dont exit.所以 post/1-3 只是不要退出。

Sorry for my stupidity对不起我的愚蠢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM