this is my views.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)
then on my webpage i use {{headlines}} to display the results but nothing shows template code
then on my webpage i use {{headlines}} to display the results but nothing shows template code
{% 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>
i have added the complete code on the template
Please use {{headline}} in your webpage because in your context you are passing "headline". I am seeing that it has many data so you can also use for loop in django template. like below....
{% for headline in headline %}
{{headline}}
{% endfor %}
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"})
context = {
"headlines": headlines
}
return render(request, 'home/maroweb.html', context)
url = 'https://inshorts.com/en/read'
response = requests.get(url)
print_headlines(response.text)
And your template will be:
{% for headline in headlines %}
{{ headline }}
{% endfor %}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.