私信  •  关注

Serafeim

Serafeim 最近创建的主题
Serafeim 最近回复了
7 年前
回复了 Serafeim 创建的主题 » 循环python脚本

使用A for 并迭代到包含所有所需值的列表。 下一步,使用 str 将值存储在字典中。

这样做:

import requests
import pandas as pd
import urllib.parse
import urllib.request
import re
import os
import sys

pagenums=[2,3,10,50]
#or pagenums = np.range(1,101)

for page in pagenums:
    querystring ={"id":"***","act":"search","***":"***","country":"",
                  "state":"*","city":"","zip":"","type":"","base":"","PAGENUM":str(page)}
    #......
    #..... # more code here

    #headers = {....}

对于每个迭代,的值 PAGENUM 键,将更新。

7 年前
回复了 Serafeim 创建的主题 » 如何在Django的图像列表中找到第一个图像

你可以用 {% if forloop.counter0 == 0 %} do something {% endif %} {% if forloop.counter == 1 %} do something {% endif %} 当使用 {% for %} 模板标记。

更多信息: https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#for

因此,您的代码应该是

<a href="#" class="result-image-act" >
  {% for image in item.images_cars_set.all %}
    {% if forloop.counter == 1 %}
      <img class="active" src=" {{image.car_images.url}}">
     {% else %}
       <img src=" {{image.car_images.url}}">
    {% endif %}
  {% empty %}
    <img src="{% static 'path/to/default image' %}" class="active">
  {% endfor %}
</a>