【问题标题】:Django template : Need 2 values to unpack in for loop; got manyDjango 模板:在 for 循环中解压需要 2 个值;有很多
【发布时间】:2019-01-09 06:27:20
【问题描述】:

所以更新我之前的问题: 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)

然后我将在 annonces 中解压缩数据以在我的模板中使用它。

                {% 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 %}

对不起我的代码风格,我是编程和 python 的新手。任何编写更好代码的专业提示都非常感谢;)

谢谢!

【问题讨论】:

  • 请分享您的模板代码。
  • 添加回溯。
  • @WillemVanOnsem 添加
  • @SachinKukreja 什么是回溯?
  • 错误堆栈跟踪。您在 cmets 中的一个答案中发布的服务器日志中的所有错误。

标签: python django templates render


【解决方案1】:

在您的模板中使用

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

【讨论】:

  • same :需要 2 个值在 for 循环中解压;得到 8. 请求方法:POST 请求 URL:127.0.0.1:8000/accounts Django 版本:2.0.3 异常类型:ValueError 异常值:需要 2 个值来解压 for 循环;得到 8. 异常位置:C:\Users\user\Anaconda3\lib\site-packages\django\template\defaulttags.py 在渲染中,第 202 行 Python 可执行文件:C:\Users\user\Anaconda3\python.exe Python 版本: 3.6.4
【解决方案2】:

有两种方法: 第一个:

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

第二个

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

【讨论】:

    猜你喜欢
    • 2019-01-09
    • 2021-03-23
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 2013-10-26
    • 2021-03-11
    • 1970-01-01
    • 2019-07-21
    相关资源
    最近更新 更多