【发布时间】:2019-12-24 06:35:00
【问题描述】:
这是我的观点.py
from django import template
from django.shortcuts import render
import requests
from bs4 import BeautifulSoup
def print_headlines(request,response_text):
soup = BeautifulSoup(response_text, 'lxml')
headlines = soup.find_all(attrs={"itemprop": "headline"})
for headline in headlines:
print(headline.text)
context = {
"headline": headline
}
return render(request, 'home/maroweb.html', context)
url = 'https://inshorts.com/en/read'
response = requests.get(url)
print_headlines(response.text)
然后在我的网页上我使用 {{headlines}} 显示结果,但没有显示模板代码
然后在我的网页上我使用 {{headlines}} 显示结果,但没有显示模板代码
{% extends "base.html" %}
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% block content %}
{{ self.banner_title }}
{% for headline in headline %}
{{headline}}
{% endfor %}
{% endblock %}
</body>
</html>
我已经在模板上添加了完整的代码
【问题讨论】:
-
尝试 {{ 标题 }} 而不是 {{ 标题 }}。您在模板中使用了额外的“s”
-
同样的结果,仍然没有在网页上显示结果
标签: python django beautifulsoup