我试图创建一个博客的标题和描述的形式和照片创建内联在admin admin StackedInline。到目前为止,一切都工作正常,包括带有帖子列表的list.html页面,但是detail.html页面将所有帖子返回到一个页面上。如何解决?这个页面包含评论,我在网上没有找到任何类似的。我希望detail.html页面只返回属于list.html中主标题的帖子
Models.py
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils import timezone
from taggit.managers import TaggableManager
from django.utils.html import mark_safe
class PublishedManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status='published')
class Post(models.Model):
STATUS = (
(0,"Draft"),
(1,"Publish")
)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=250, unique_for_date='publish')
publish = models.DateTimeField(default=timezone.now)
description = models.TextField()
image = models.ImageField(upload_to ='uploads/')
status = models.IntegerField(choices=STATUS, default=0)
objects = models.Manager() # The default manager.
published = PublishedManager() # Our custom manager.
#tags = TaggableManager(blank=True)
def img_preview(self): #new
return mark_safe(f'<img src = "{self.image.url}" width = "300"/>')
def __str__(self):
return self.title
class Travel(models.Model):
travel = models.ForeignKey(Post, on_delete=models.CASCADE, verbose_name="travel-post")
title = models.CharField(max_length=200)
description = models.TextField()
image = models.ImageField(upload_to ='uploads/')
created = models.DateTimeField(auto_now_add=True)
objects = models.Manager() # The default manager.
class Comment(models.Model):
STATUS = (
('Lido', 'Lido'),
('Não Lido', 'Não Lido'),
)
post = models.ForeignKey(Post,
on_delete=models.CASCADE,
related_name='comments')
name = models.CharField(max_length=80)
email = models.EmailField()
body = models.TextField(max_length=800)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(choices=STATUS, max_length=10, default="Não Lido")
admin.py
from django.contrib import admin
from .models import Travel, Post, Comment
class TravelInline(admin.StackedInline):
model = Travel
extra = 0
list_display = ('title', 'user')
list_filter = ( 'created', 'user')
search_fields = ('title', 'description')
date_hierarchy = 'created'
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
inlines = [TravelInline]
list_display = ['title', 'user', 'status']
prepopulated_fields = {'slug': ('title',)}
@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
list_display = ('name', 'email', 'post', 'created', 'status')
list_filter = ('status', 'created', 'updated')
search_fields = ('name', 'email', 'body')
views.py
from django.shortcuts import render, redirect
from .models import Travel, Post, Comment
from django.shortcuts import render, get_object_or_404
from django.db.models import Count
from .forms import EmailPostForm, CommentForm
# Create your views here.
def post_list(request):
posts = Post.objects.all()
post_latest = Post.objects.order_by('id')[:3]
return render(request, 'travel/post/list.html', {
'posts': posts,
'post_latest': post_latest,
})
def post_detail(request, id, slug):
post = Post.objects.get(pk=id)
travel = Travel.objects.all()
comments = Comment.objects.filter(post_id=id, status='Lido')
total = 0
for i in comments:
total = total + 1
context = {
'post': post,
'comments': comments,
'travel': travel,
'total': total,
}
return render(request, 'travel/post/post_detail.html', context)
list.html
{% extends "travel/base.html" %}
{% block title %}Travel{% endblock %}
{% block content %}
{% for p in posts %}
<h2 class="mb-0 text-capitalize"><b>{{ p.title }}</b></h2>
<div class="mb-1 text-muted"><h6>Published {{ p.publish }} by {{ p.user }}</h6></div>
<p class="card-text">{{ p.description|truncatewords:20|linebreaks }}</p>
<a href="/post/{{p.id}}/{{p.slug}}"
class="stretched-link text-primary"><h5>Continue reading</h5></a>
{% if p.image %}
<div class="col-auto d-none d-lg-block py-5">
<figure class="image">
<img src="{{ p.image.url }}" class="img-fluid" width="400" height="200">
</figure>
</div>
{% endif %}<br>
{% endfor %}
{% endblock %}
detail.html
{% extends "travel/base.html" %}
{% block title %}{{ post.title }}{% endblock %}
{% block content %}
{% if post.image %}
<div class="p-4 p-md-2 mb-4 rounded text-bg-dark"><img class="img-fluid" src="{{ post.image.url }}">
{% endif %}
<div class="col-md-6 py-3">
<h1 class="display-4 fst-italic text-capitalize">{{ post.title }}</h1>
<p class="lead my-3">{{ post.description }}.</p>
</div>
<p class="date">
Published {{post.publish }} by {{ post.User }}
</p>
{% for t in travel %}
<div class="position-relative overflow-hidden text-center bg-light">
<div class="p-lg-5 mx-auto">
<h1 class="display-4 fw-normal text-capitalize">{{ t.title }}</h1>
<p class="lead fw-normal">{{ t.description|linebreaks }}</p>
</div>
{% if t.image %}
<div class="p-4 p-md-2 mb-4" ><img class="img-fluid" src="{{ t.image.url }}" width="600"></div>
{% endif %}
{% endfor %}
{% for comment in comments %}
<div class="card border-secondary mb-3" style="max-width: 40rem; justify-content-evenly d-flex flex-column position-static">
<div class="card-header">Comment {{ forloop.counter }} by <span class="text-danger"> {{ comment.name }} </span>
</div>
<div class="card-body">
{{ comment.created }}
<p class="card-text">{{ comment.body|linebreaks }}</p>
</div>
</div>
</div>
</div>
</div>
{% empty %}
<p>There are no comments yet.</p>
{% endfor %}
{% endblock %}
我认为错误是travel = views.py中的travel .objects.all()我该如何解决这个问题?
我通过改变来解决这个问题;
travel = Travel.objects.all()
travel = Travel.objects.all().filter(pk=id)