1

I need something like file_get_contents in PHP. I kwnow it's some like:

import urllib2
urllib2.urlopen(url).read()

but how can I put it in django html template?

Thx for fast answers.

Kamilos
  • 795
  • 2
  • 10
  • 25
  • 3
    You don't put this in template because template does rendering, and this is data-pulling. So you do that in your view, assign result to template context, and then render it in template. – Ski Nov 18 '11 at 13:17
  • ok, I add this to view to var: tpldata['somecode']. now how can I put it into template? – Kamilos Nov 18 '11 at 13:25
  • 1
    Yes you can. Easiest way to render a template is `render_to_response` function in `django.shortcuts`. Variables in template are rendered like this: `{{ somevar|safe }}`. The `safe` keyword tells template-engine that this is html code and it is safe (it will not be escaped). Check django-template language documentation for more details https://docs.djangoproject.com/en/dev/topics/templates/. Also have a look at builtin tags: https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs – Ski Nov 18 '11 at 14:21

1 Answers1

2

You cannot put that directly into django's html templating language. You might consider:

  • Prefetching the html, store it somewhere and render it to the template.
  • Use an ajax call, that triggers a view that does the crawling, then on success you insert your html in the DOM (this is blocking and slow at server-side).
  • If you truly want this to be non-blocking, use a non-blocking server. See this code as an example.
Community
  • 1
  • 1
hymloth
  • 6,869
  • 5
  • 36
  • 47