如何使用 django 和 JSONP 从我的其他网站加载内容?

发布于 2024-08-02 23:25:04 字数 270 浏览 3 评论 0原文

我有两个用 Django 编写的网站。我想要的是将一个网站的内容加载到另一个网站。我发现最好的选择是使用 .getJSONJSON-P 但我不知道如何将这些东西(Django、jQuery 和 JSONP)放在一起。

有更多经验丰富的用户提供帮助吗?

编辑

我试图实现一个持续的过程,在每个页面浏览中将内容从一个网站加载到另一个网站 - 而不是单个转储/加载数据内容。

I have two sites written with Django. What I want is to load a content from one site into another. I found out that best choice to do this would be using .getJSON and JSON-P but I have no idea how to put this things (Django, jQuery and JSONP) together.

Any help from more expirienced users?

EDIT

I'm trying to achive an ongoing process with things being loaded from one site into another on every pageview - not a single dump/loaddata stuff.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蹲在坟头点根烟 2024-08-09 23:25:04

要公开 JSONP 端点,请使用 django-rest-interface

要从其他站点加载它,请使用 Jquery 的 getJSON

To expose your JSONP endpoint use django-rest-interface.

To load it from the other site use Jquery's getJSON.

半透明的墙 2024-08-09 23:25:04

最好的解决方案是将两个站点放在同一个数据库中。然后site2可以简单地读取site1的数据库。事实上,site2可以包含site1应用程序,使得site2包含site1的所有功能。

如果由于某种原因无法让 site1 和 site2 共享公共数据库,则 site2 必须通过 HTTP 请求来从 site1 获取数据。

在 site2 应用程序中,您使用与 site1 相同的 urls.py。然而,site2 视图函数使用 urllib2 向 site1 发出 HTTP GET 和 POST 请求。一旦 site2 视图函数收到来自 site1 的响应,它就会简单地返回它。

您的 site2 应用程序可能看起来像这样。

def someSite2View( request ):
    site1= urllib2.open( "http://site1/" + request.path  )
    data= site1.read()
    # you have to parse the data to extract the headers
    return Response( headers and data )

这是令人惊讶的可行的。这种转发在 HTTP 管道中经常发生。

The best solution is to put both sites in the same database. Then site2 can simply read site1's database. Indeed, the site2 can include the site1 application programs, making site2 include all of the site1 features.

If, for some reason, you can't get site1 and site2 to share a common database, then site2 must get data from site1 by requesting it through HTTP.

In the site2 application, you use the same urls.py as site1. The site2 view functions, however, use urllib2 to make HTTP GET and POST requests to site1. Once the site2 view function has the response from site1, it simply returns it.

Your site2 applications can look something like this.

def someSite2View( request ):
    site1= urllib2.open( "http://site1/" + request.path  )
    data= site1.read()
    # you have to parse the data to extract the headers
    return Response( headers and data )

This is suprisingly workable. This sort of forwarding happens a lot in HTTP pipelines.

你怎么这么可爱啊 2024-08-09 23:25:04

jQuery.ajax() 通过 JSONP 请求 json 数据。您只需在选项中指定 dataType 为“jsonp”即可。在服务器端,您只需处理 jsonp 请求。例如,您可以使用 django-jsonp 模块,它提供了使用 jsonp 所需的几乎所有内容Django 中的数据。

jQuery.ajax() to request a json data via JSONP. You just need to specify that the dataType is 'jsonp' in options. On the server side you just have to handle a jsonp request. For example, you can use django-jsonp module, which provides nearly everything you need to work with jsonp data in django.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文