How to skip results and save them in a list so I can use them in my Django template

I tried to fully process the object and save and save the results in a list so that I could use it in my django template and display it. I tried to copy what I have elsewhere in my code to satisfy my needs, but it does not work. I tried to duplicate this

{% for tag in instance.tags.all %}
      {{ tag.post_set.all }}
{% endfor %}

this returns everything in one block. I want to be able to get through it, so I tried this

links = []
for t in tag:
   links.append(
       t.post_set.all()
   )

mylink = links
context = {
    "title": "detail ",
    "instance": instance,
    "hops": mylink
}

but it didn’t work. What is the correct syntax to break my results from a loop and save them in a list that I can use in my template. Any help or advice is appreciated.

EDIT:

my opinion

 def post_detail(request, slug=None):
    instance = get_object_or_404(Post, slug=slug)

    tag = instance.tags.all
    links = []
    for t in tag:
       links.append(
           t.post_set.distinct()
       )

   share_string = quote_plus(instance.content)
   tag = instance.tags.all()
   context = {
       "title": "detail ",
       "instance": instance,
       "share_string": share_string,
       "tag": tag
   }
   return render(request, "posts/post_detail.html", context)



 class Tag(models.Model):
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=200, unique=True)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)

    def __str__(self):
        return self.title

   def get_absolute_url(self):
        return reverse("posts:tag_index", kwargs={"slug": self.slug})

   class Meta:
        ordering = ["-timestamp"]


class Post(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    slug = models.SlugField(unique=True)
    title = models.CharField(max_length=120)
    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)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    tags = models.ManyToManyField(Tag)

    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"]


def create_slug(instance, new_slug=None):
    slug = slugify(instance.title)
    if new_slug is not None:
        slug = new_slug
    qs = Post.objects.filter(slug=slug).order_by("-id")
    exists = qs.exists()
    if exists:
        new_slug = "%s-%s" % (slug, qs.first().id)
       return create_slug(instance, new_slug=new_slug)
    return slug


def pre_save_post_receiver(sender, instance, *args, **kwargs):
    if not instance.slug:
        instance.slug = create_slug(instance)


pre_save.connect(pre_save_post_receiver, sender=Post)

this is what i have

+4
3

, .

links = Post.objects.filter(link__tag__instancemodel=instance)

instancemodel - instance.

0

:

{% for tag in instance.tags.all %}
      {% for post in tag.post_set.all %}
          {{ post }}
      {% endfor %}
{% endfor %}
+2

. alecxe .

-, , . Python. distinct()

links = []
for t in tag:
   links.append(
       t.post_set.distinct()
   )

. instance.tags , , , () .

def post_detail(request, slug=None):
    instance = get_object_or_404(Post, slug=slug)

    tags = instance.tags.distinct()

    share_string = quote_plus(instance.content)

    context = {
        "title": "detail ",
        "instance": instance,
        "share_string": share_string,
        "tags": tags
    }
    return render(request, "posts/post_detail.html", context)

:

{% for tag in tags %}
      {{ tag }}
{% endfor %}
+1
source

Source: https://habr.com/ru/post/1626890/


All Articles