因此,要更新我刚才的问题: Django的模板:需要2个值来解压在for循环; 拿到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 %}
对不起,我的代码风格我在编程和蟒蛇新。 编写更好的代码的任何PRO提示都大加赞赏;)
THKS!