Django template : Need 2 values to unpack in for loop; got many

烈酒焚心 提交于 2019-12-11 17:46:52

问题


So to update my previous question : Django template : Need 2 values to unpack in for loop; got 8

from django.shortcuts import render, redirect   
from accounts.forms import Searchform 
import requests

page=''
ville =''
region=''
prixmin =''
prixmax= ''
surfacemin='' 
surfacemax=''
def post(request):
    global page ,ville ,prixmin ,prixmax, surfacemin, surfacemax, region, annonces
    if request.method == 'POST':    
        form = Searchform(request.POST)
        if form.is_valid():
            ville = form.cleaned_data['ville']          
            prixmin = form.cleaned_data['prix_min']
            prixmax = form.cleaned_data['prix_max']
            surfacemax = form.cleaned_data['surface_max']
            surfacemin = form.cleaned_data['surface_min']
    else:
        page='1'
        form = Searchform()

    annonces = []

    try:
        url = 'example'
        img = 'example'
        ville = 'example'
        typeImmo = 'example'    
        Nb_piece = 'example'
        Nb_ch = 'example'
        surface = 'example'
        prix = 'example'
        annonces.append((url,img,ville,typeImmo,Nb_piece,Nb_ch,surface,prix))
    except:pass

    args = {'form': form, 'annonces':annonces, 'rech':len(annonces)}
    return render(request, 'accounts/page_recherche.html', args)

and then I will unpack the data in annonces to use it in my template.

                {% for annonce in annonces %}
            <div class="col">
                <div class="card" style="width: 33rem;">
                    <div class="row">

                        <div class="col">
                              <a href="{{annonce.0}}" target="_blank">
                                <img class="card-img-top" src="{{annonce.1}}" alt="No image" height="180">
                              </a>
                        </div>

                        <div class="col">     
                          <ul class="list-group list-group-flush">
                            <li class="list-group-item">{{annonce.2}}</li>
                            <li class="list-group-item">{{annonce.6}} - {{annonce.4}}</li>
                            <li class="list-group-item">{{annonce.7}}</li>
                          </ul>
                       </div>
                </div>      
            </div>
            {% endfor %}

Sorry for the style of my code I'm new at programming and at python. Any pro tips for writing better code are much appreciated ;)

Thks !


回答1:


In your template use

{% for i in annonces %}
    <h3>{{i.0}}</h3>
    <h3>{{i.1}}</h3>
    #and so on
    #..
    <h3>{{i.8}}</h3>
{% endfor %}



回答2:


There is two ways: First one:

{% for i in annonces %}
    <h3>{{i.0}}</h3>
    <h3>{{i.1}}</h3>
    #and so on
    #..
    <h3>{{i.7}}</h3>
{% endfor %}

Second one

{% for a,b,c,d,e,f,g,h in annonces %}
    <h3>{{a}}</h3>
    <h3>{{a}}</h3>
    #and so on
    #..
    <h3>{{h}}</h3>
{% endfor %}


来源:https://stackoverflow.com/questions/51637418/django-template-need-2-values-to-unpack-in-for-loop-got-many

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!