【问题标题】:How do I access the data from inside a nested for loop如何从嵌套的 for 循环中访问数据
【发布时间】:2021-12-15 10:33:24
【问题描述】:

我想在下面的 html 中显示多个“点”,并在每个点内显示与每个特定点关联的“链接”。

如何编写逻辑来显示每个位置的特定链接?

html

{% for spot in spots %}
    <div>
      <h2>{{ spot.title }}</h2>
    </div>
    {% for link in links %}
    <div>
      <h3>{{ link.url }}</h3>
    </div>
    {% endfor %}
{% endfor %}

我的模型如下。 SpotLinks 是 Links 和 Spots 表的中间表。

models.py

class Spots(models.Model):
    title = models.CharField(max_length=155)
    slug = models.SlugField(unique=True, max_length=155)


class Links(models.Model):
    title = models.CharField(unique=True, max_length=155, blank=True)
    url = models.CharField(max_length=75, blank=True)


class SpotLinks(models.Model):
    link = models.ForeignKey('Links', models.DO_NOTHING)
    spot = models.ForeignKey('Spots', models.DO_NOTHING)


class Articles(models.Model):
    title = models.CharField(max_length=155)
    slug = models.SlugField(unique=True, max_length=155)
    summary = models.TextField(blank=True, null=True)


class ArticleSpots(models.Model):
    article = models.ForeignKey('Articles', models.DO_NOTHING)
    spot = models.ForeignKey('Spots', models.DO_NOTHING)

我在views.py 中尝试了links = Links.objects.filter(spotlinks__spot=spots),但由于斑点中有多个斑点,它没有被过滤。

逻辑是写在views.py中还是作为django模板写在html文件中?

我是 Web 开发的新手,所以任何方向都会有所帮助。谢谢!

views.py

def article(request, slug):
    article = get_object_or_404(Articles, slug=slug)
    spots = Spots.objects.filter(articlespots__article=article).distinct()

    links = Links.objects.filter(spotlinks__spot=spots)

    context = {
        'spots': spots,
        'article': article,
        'links': links}

    return render(request, 'articletemplate.html', context)

【问题讨论】:

  • for link in links link.urlfor spotlink in spot.spotlinks_set.all() spotlink.link.url
  • 谢谢@aaron。我试过了,它给了我以下错误.. TemplateSyntaxError 无法解析剩余部分:'spot.spotlinks_set.all()'中的'()'
  • 试试spot.spotlinks_set.all 不带()

标签: python django django-models django-views django-templates


【解决方案1】:

要访问相关对象,请使用相关管理器。

在这种情况下,相关经理是spot.spotlinks_set

{% for spot in spots %}
    <div>
      <h2>{{ spot.title }}</h2>
    </div>
    <!-- {% for link in links %} -->             <!-- Replace this -->
    {% for spotlink in spot.spotlinks_set.all %} <!-- with this    -->
    <div>
      <!-- <h3>{{ link.url }}</h3> --> <!-- Replace this -->
      <h3>{{ spotlink.link.url }}</h3> <!-- with this    -->
    </div>
    {% endfor %}
{% endfor %}

参考:https://docs.djangoproject.com/en/3.2/ref/models/relations/

【讨论】:

    【解决方案2】:

    您需要在Links模型中添加一个外部字段来引用点,因此Links模型将是:

    class Links(models.Model):
        title = models.CharField(unique=True, max_length=155, blank=True)
        url = models.CharField(max_length=75, blank=True)
        spot = models.ForeignKey(Spots, on_delete=models.CASCADE, related_name="links")
    

    因此,您可以通过以下方式获取每个景点的链接:

    from django.shortcuts import get_object_or_404
    spot = get_object_or_404(Spots, title="choose the the title") # you can filter it in your preferred way
    links = spot.links.all()
    

    然后,在上下文中;您根本不需要发送链接,而是可以通过这种方式在 html 中进行迭代:

    {% for spot in spots %}
        <div>
          <h2>{{ spot.title }}</h2>
        </div>
        {% for link in spot.links %}
        <div>
          <h3>{{ link.url }}</h3>
        </div>
        {% endfor %}
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      相关资源
      最近更新 更多