如何在项目符号列表格式的 Django 模板中显示 python 字典



如何更改以下 Django 模板代码:

<ul> 
 {% for key, value in real_prices.items %} #real_prices is a dictionary
   <li>Price: {{ key }}, link: {{value}}</li>
 {% endfor %}
</ul>

要在如下所示的页面上显示字典项目,请执行以下操作:

  • 价格:键,链接:值
  • 价格:键,链接:值
  • 价格:键,链接:值
  • 等等...

当前代码仅显示整个字典。

作为参考,以下是视图:

def test(request):
    prices = []
    real_prices = []
    links = []
    url = 'https://www.airbnb.pl/s/Girona--Hiszpania/homes?refinement_paths%5B%5D=%2Fhomes&query=Girona%2C%20Hiszpania&place_id=ChIJRRrTHsPNuhIRQMqjIeD6AAM&checkin=2018-04-04&checkout=2018-04-22&children=0&infants=0&adults=2&guests=2&allow_override%5B%5D=&price_max=252&room_types%5B%5D=Entire%20home%2Fapt&min_beds=0&s_tag=Ph6ohhjw'
    response = requests.get(url)
    soup = bs4.BeautifulSoup(response.text)
    #prices
    page_selectors = soup.select('._1bdke5s')
    print(len(page_selectors))
    last_page_selector = page_selectors[len(page_selectors) - 4]##############-15
    last_page_selector = last_page_selector.getText()
    for x in range(0, int(last_page_selector)):
        response = requests.get(url + '&section_offset=' + str(x))
        response_text = response.text
        soup = bs4.BeautifulSoup(response_text)
        spans = soup.select('._hylizj6 span')
        for i in range(0, len(spans)):
            prices.append(spans[i].getText())
        for price in prices:
            if 'zł' in price:
                real_prices.append(price)
        #links
        a_tags = soup.find_all('a')
        for tag in a_tags:
            if '_15ns6vh' in str(tag.get('class')):
                link = 'https://www.airbnb.pl' + str(tag.get('href'))
                links.append(link)
    dictionary = dict(zip(real_prices, links))
    context = {'dictionary': dictionary}
    return render(request, 'javascript/test.html', context)

real_prices替换为dictionary,它将起作用。上下文字典中没有名为 real_prices 的键,因此它不起作用。

<ul> 
    {% for key, value in dictionary.items %}
        <li>Price: {{ key }}, link: {{ value }}</li>
    {% endfor %}
</ul>

最新更新