0

I want to get the contents of one of my pages on an external site, the HTML and all contents. If were in .NET I could use WebClient and retrieve the page, save it to a variable, and emit it in Razor. PHP can use cURL.

How can I do that in a Django template? Do I need to create a Plugin that uses urllib?

If it matters, I am using DjangoCMS. I searched and tried the Django http module.

I have looked at the helper Django module for http and didn't see anything. I wasn't sure where to put urllib or requests, as in I do not know if what I am trying to do requires me to build a plugin or a custom template tag.

I was hoping to know how to do it with either Python and Django's template language or just the template language, so I really do not want to do this in JavaScript, iframe, object, etc.

johnny
  • 19,272
  • 52
  • 157
  • 259

1 Answers1

5

You can use the reqeusts library, and define a function as a part of a view.

Alternatively you can define a simple tag if you want it to be globally accessible.

For instance

import requests
from django import template

register = template.Library()

@register.simple_tag
def get_site_source(url):
    res = requests.get(url)
    return res.text

and in the template:

...
{% get_site_source url %}
...
Dash Winterson
  • 1,197
  • 8
  • 19