В HTML comment_set.all используется для получения данных из модели, где их нет. Какова концепция этого.
post_detail.html
{% extends "base.html" %}
...
<div>
{{ instance.comment_set.all }}
</div>
...
OUTPUT
[<Comment: User_Name >]
Коды, поддерживающие это
Модель приложения для комментариев приведена ниже
комментарии / models.py
from django.conf import settings
from django.db import models
# Create your models here.
from posts.models import Post
class Comment(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
post = models.ForeignKey(Post)
content = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.user.username)
сообщений / models.py
...
class Post(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
title = models.CharField(max_length=120)
slug = models.SlugField(unique=True)
image = models.ImageField(upload_to=upload_location,
null=True,
blank=True,
width_field="width_field",
height_field="height_field")
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
content = models.TextField()
draft = models.BooleanField(default=False)
publish = models.DateField(auto_now=False, auto_now_add=False)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
objects = PostManager()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("posts:detail", kwargs={"slug": self.slug})
class Meta:
ordering = ["-timestamp", "-updated"]
def get_markdown(self):
content = self.content
markdown_text = markdown(content)
return mark_safe(markdown_text)
...
сообщений / views.py
...
def post_detail(request, slug=None):
instance = get_object_or_404(Post, slug=slug)
if instance.publish > timezone.now().date() or instance.draft:
if not request.user.is_staff or not request.user.is_superuser:
raise Http404
share_string = quote_plus(instance.content)
context = {
"title": instance.title,
"instance": instance,
"share_string": share_string,
}
return render(request, "post_detail.html", context)
...
Примечание в views.py posts_detail.html отображается с помощью post_detail ()
но в posts_detail.html мы обращаемся к данным комментариев в нем. Как это возможно, поскольку в файле posts / models.py отсутствует комментарий к комментарию в comments / models.py