如何使post_detail页面正常工作?

时间:2018-10-05 05:50:56

标签: django

以下是我的urls.py文件:

from django.urls import path, re_path, include
from . import views

app_name = 'blog'

urlpatterns = [
    path('', views.post_list, name='list'),
    path('<slug>/', views.post_detail, name='detail'),
]

我的views.py文件:

from django.shortcuts import render
from .models import Post


# Create your views here.

def post_list(request):
    posts = Post.objects.all().order_by('date')
    return render(request, 'blog/post_list.html', {'posts': posts})

def post_detail(request, slug):
    post_det = Post.objects.get(slug=slug)
    return render(request, 'blog/post_detail.html', {'singlepost': post_det})

还有我的post_detail.html页面:

{% extends 'base.html' %}

{% block content %}

    <div class="container-fluid">
        <div class="row">
              {% for post in posts %}
              <div class="post col-md-10 shadow mx-auto">
                <!-- post-thumbnail -->
                <div class="post-thumbnail">
                    <img src="{{singlepost.thumb.url}}">
                </div>
                <!-- /post-thumbnail -->
                <h2 class="post-title bg-dark text-light pad1 text-center">{{ singlepost.title }}</h2>
                <p class="post-content">{{ singlepost.body }}</p>
                <p class="post-info grey border pad1">{{ singlepost.date }}</p>
               </div>
              {% endfor %}
        </div>
    </div>


{% endblock content %}

错误页面上的屏幕截图: Django - Page Not Found Error

这里似乎是什么问题?请记住,我是django的新手,但这是我第一次遇到类似错误报告中提到的URL的URL。

1 个答案:

答案 0 :(得分:0)

我做了几处更改, 在urls.py中,

path('detail/<slug:slug>/', views.#restofyoururl ),

您尚未添加首页html,因此请检查您是否已正确添加网址。在您的首页html

<a href={% url 'blog:detail' post.slug %}> {{post.title}}#add as per your model </a>

注意:您可以为您的详细信息页面添加基于类的视图。