如何在Django模板的内部for循环中使用外部for循环值

时间:2019-10-23 08:07:31

标签: django django-templates

我正在做Django Project。在这里,我尝试在Django模板的内部for循环中使用Outer for循环数据。帮助我,因为我在内部for循环中犯了错误,我不知道该如何解决。

views.py

def test_view(request):
    if 'username' in request.session:
        if request.method == 'GET':
            offers_objs = Offers.objects.all().values()
            data = Signup.objects.all().values()
            return render(request, 'index.html',{'offers_objs':offers_objs, 'data': data})

index.html

{% for i in offers_objs %}
    <div class="divi" style="height: 410px">
        <img src="{{ i.image.url }}" alt="Images" width="300px" height="auto"/>
             <p>Offer Des: {{ i.description }}</p>
             <p>Address: {{ i.address }}</p>
             <p>Offer id: {{ i.offer_id }}</p>
    </div>
        {% for {{ i.username }} in data %}
            <p>{{ name }}</p>
        {% endfor %}
{% endfor %}

models.py

class Signup(models.Model):
    name = models.CharField(max_length=50, blank=True, null=True)
    email = models.EmailField(max_length=50, unique= True)
    phone_number = models.CharField(max_length=12, unique= True)
    username = models.CharField(max_length=50, unique= True)
    password = models.CharField(max_length=50, blank=True, null=True)
    address = models. CharField(max_length=50, blank=True, null=True)

class Offers(models.Model):
    offer_id = models.CharField(max_length=100, blank=True, null=True)
    description = models.CharField(max_length=100, blank=True, null=True)
    username = models.CharField(max_length=100, blank=True, null=True)

1 个答案:

答案 0 :(得分:-1)

删除{{ }}中的{% for {{ i.username }} in data %}应该可以解决。

{% for i in offers_objs %}
    <div class="divi" style="height: 410px">
        <img src="{{ i.image.url }}" alt="Images" width="300px" height="auto"/>
             <p>Offer Des: {{ i.description }}</p>
             <p>Address: {{ i.address }}</p>
             <p>Offer id: {{ i.offer_id }}</p>
    </div>
        {% for i.username in data %}
            <p>{{ name }}</p>
        {% endfor %}
{% endfor %}