問題

我在django 2.2中建立一個帶有部落格應用程式的網站.我只有最後一步遇到問題,即為單個帖子建立檢視.不幸的是,我不知道如何完成它.

我建立了我可以的最簡單檢視,但輸入後,我從伺服器收到500錯誤.

  def single_post(request):
     return render(request=request, template_name="posts/single_post.html")
 

這是我的模型,檢視和網址:

models.py

 # Post models
from django.db import models
from datetime import datetime
from django.utils.text import slugify

class Post(models.Model):
    title = models.CharField(max_length=150, verbose_name="Tytuł")
    content = models.TextField(verbose_name="Zawartość")
    lead = models.CharField(max_length=35, verbose_name="Skrót artykułu")
    published = models.DateTimeField(verbose_name="Data publikacji", default=datetime.now())
    slug = models.SlugField(unique=True)
    cover = models.ImageField(upload_to='images/')

    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super(Post, self).save(*args, **kwargs)

    def __str__(self):
        return self.title
 

urls.py

 from django.contrib import admin
from django.urls import path, include
from . import views

app_name = "posts"

urlpatterns = [
    path("posts/", views.posts_list, name="posts_list"),
    path('<str:slug>/', views.single_post, name='single_post'),]
 

views.py

 from django.shortcuts import render
from django.http import HttpResponse
from .models import Post

# Create your views here.

def posts_list(request):
    return render(request=request, template_name="posts/posts_list.html", context={"posts": Post.objects.all})

def single_post(request,slug):
    post = Post.objects.get(slug=slug)
    return render(request=request, template_name="posts/single_post.html", context={"post": post})
 

模板/帖子/posts_list.html – 是一個頁面,所有帖子都在exmaple.com/posts/

 <section class="bg-light py-5" id="aktualnosci">
<div class="container">
    <h1>Informacje o zmianach w prawie podatkowym</h1>
    <div class="divider"></div>
    <p class="text-paragraph pt-3">Lorem ipsum dolor sit amet consectetur adipisicing elit. Ullam iure consectetur accusantium delectus, iusto culpa mollitia eum molestiae at? Ab!</p>
    <div class="row py-3">
        {% for post in posts %}
        <div class="col-lg-6">
            <div class="news-card mb-3">
                <div class="text-center text-white bg-blue d-flex align-items-center news-card-date">
                    <div class="mx-auto news-card-date-body w-75">
                        <i class="far fa-calendar-alt d-none d-block mx-auto"></i>
                        <span class="d-block news-card-date-value mt-1">{{ post.published }}</span>
                    </div>
                </div>
                <div class="news-card-body">
                    <div class="news-card-img">
            <img class="img-fluid" src="{% static 'main/images/126.jpg' %}" alt="">
                    </div>
                    <div class="news-card-content">
                        <div class="news-card-content-inner">
                            <h2>{{ post.title }}</h2>
                            <p class="text-paragraph">{{ post.lead }}</p>
                            <a class="pb-2" href="{{ post.slug }}">Czytaj więcej</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
</div>
 

我希望網站的訪問者能夠閱讀整篇文章.目前,我只能建立所有帖子和捷徑的整個頁面.

  最佳答案

您需要為此傳遞帖子pk

 def single_post(request,pk):
    post = Post.objects.get(pk=pk)
    return render(request=request, template_name="posts/single_post.html",{'post':post})
 

在模板中你可以得到這樣的細節

 <p>{{post.title}}</p>
 

並將pk作為url引數傳遞給url中的詳細資訊

 path('posts/<int:pk>',views.single_post,name='single_post')
 

您沒有為帖子詳細網址提供正確的網址

 <h2>{{ post.title }}</h2>
   <p class="text-paragraph">{{ post.lead }}</p>
   <a class="pb-2" href="{% url 'posts:single_post' post.slug %}">Czytaj więcej</a>
 

  相同標籤的其他問題

pythondjango