Retrieving RSS xml News data using Jquery/Ajax in Django

陌路散爱 提交于 2020-01-04 09:13:46

问题


I am trying to do the following:

I need to download the headlines from BBC and CNN using Ajax and jquery in Django. I tried first to download the data in javascript but I am getting a 'Access-Control-Allow-Origin' so i worked out I needed to do this through the backend in django. this needs to be done synchronously so that the browser doesn't have to be refreshed to redownload the data.

The requirements for my program are:

  • Build a web application that displays, side by side, the headlines from both BBC and CNN, using their RSS (XML) feeds.
  • Your application should use jQuery’s support for Ajax.
  • The Django backend should download the news from:

• http://feeds.bbci.co.uk/news/rss.xml
• http://rss.cnn.com/rss/cnn_topstories.rss

and serve those to the client upon the ajax request for a news update.

So far I have the following:

from django.shortcuts import render
import requests


def index(request):
    context = {}
    return render(request, 'home/Newshome.html', context)


def submit(request):
    xml_news = requests.get('http://rss.cnn.com/rss/cnn_topstories.rss')
    news = xml_news.content
    return render(request, 'home/Newshome.html', {'news': news})
<!DOCTYPE html>
<html>
	<head>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
        $(document).ready(function() {
            $.ajax({
                type: "GET",
                url: "http://rss.cnn.com/rss/cnn_topstories.rss",
                dataType: "xml",
                success: function upon_success(xml) {
                    $(xml).find('item').each()
                }
            });
        });
        </script>
	</head>
	<body>
		<h1>Top News: BBC versus CNN</h1>
		{% for item in news %}
           <li>{{ item }}</li>
        {% endfor %}
	</body>
</html>

Any help would be appreciated! or pointers to links that will help me

来源:https://stackoverflow.com/questions/42326777/retrieving-rss-xml-news-data-using-jquery-ajax-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!