Generating server-side off-line HTML of Django pages ...

37 views
Skip to first unread message

Bernd Wechner

unread,
Jun 4, 2018, 2:00:09 AM6/4/18
to Django users
Say I have a page on my Django website (because I do) that I would like to take a snapshot of on an automated basis on the server itself with a crontab say. I imagine writing a small python script that I could run, that loads Django, a settings file, knows a URL and has a way of saying "give me the rendered page for that URL please" and save it in a file.

On way of course is open port 80 or 443, send an HTTP request and get the HTTP response. But there is probably a better way that leaves the web and UWSGI servers right out of the equation and just runs Django template rendering code or a view itself and nabs the result.

As usual, I'm both confident this is possible and that I'll nut out a way to do it, but am throwing it out there in case:

a) there's a canonical way to do this already that can be recommended
b) I can save myself some time reading and experimenting if anyone whose done this already shares their experience

I will raise the bar one on the original question while I'm here, and raise the spectre of autorun on page load javascript that the page may have. Makes it hairier by a step. But it's not unusual for javascript to do some rendering on page load - I have a page or two like that ;-), In that case neither of these strategies are likely to work and either one might if coupled with what would basically need to be, a rendering engine like mozilla's. Does Python offer that I wonder, a way to pump a response (with embedded Javascript) into a rendering engine and then save (or request) the rendered page?

Kind regards,

Bernd.

Melvyn Sopacua

unread,
Jun 4, 2018, 6:09:25 AM6/4/18
to django...@googlegroups.com

On maandag 4 juni 2018 08:00:08 CEST Bernd Wechner wrote:

> Say I have a page on my Django website (because I do) that I would like to

> take a snapshot of on an automated basis on the server itself with a

> crontab say. I imagine writing a small python script that I could run, that

> loads Django, a settings file, knows a URL and has a way of saying "give me

> the rendered page for that URL please" and save it in a file.

 

...

 

> a) there's a canonical way to do this already that can be recommended

 

Yep. The test client ... if your template uses request related information (like logged in user). The cheaper method is something like this code, but this only renders the template and you'd have to provide a context so view code is bypassed.

 

--

Melvyn Sopacua

Bernd Wechner

unread,
Jun 4, 2018, 7:14:11 PM6/4/18
to Django users
Thanks Melvyn, looks exactly like what I wanted! Will investigate. I had a feeling this could not be a novel or unique use case.

Regards,

Bernd.

Anthony Flury

unread,
Jun 6, 2018, 12:26:06 PM6/6/18
to django...@googlegroups.com
Does the test client execute javascript as well ? I can't remember.

On 05/06/18 00:14, Bernd Wechner wrote:
> Thanks Melvyn, looks exactly like what I wanted! Will investigate. I
> had a feeling this could not be a novel or unique use case.
>
> Regards,
>
> Bernd.
>
> On Monday, 4 June 2018 20:09:25 UTC+10, Melvyn Sopacua wrote:
>
> On maandag 4 juni 2018 08:00:08 CEST Bernd Wechner wrote:
>
> > Say I have a page on my Django website (because I do) that I
> would like to
>
> > take a snapshot of on an automated basis on the server itself with a
>
> > crontab say. I imagine writing a small python script that I
> could run, that
>
> > loads Django, a settings file, knows a URL and has a way of
> saying "give me
>
> > the rendered page for that URL please" and save it in a file.
>
> ...
>
> > a) there's a canonical way to do this already that can be
> recommended
>
> Yep. The test client
> <https://docs.djangoproject.com/en/2.0/topics/testing/tools/#the-test-client>...
> if your template uses request related information (like logged in
> user). The cheaper method is something like this code
> <https://github.com/melvyn-sopacua/django_xtc/blob/master/xtc/__init__.py#L49>,
> but this only renders the template and you'd have to provide a
> context so view code is bypassed.
>
> --
>
> Melvyn Sopacua
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/850bfcaa-0518-48ef-a827-087100efda92%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/850bfcaa-0518-48ef-a827-087100efda92%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--
--
Anthony Flury
email : *Anthon...@btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*

Melvyn Sopacua

unread,
Jun 6, 2018, 2:09:34 PM6/6/18
to django...@googlegroups.com
On woensdag 6 juni 2018 18:25:35 CEST 'Anthony Flury' via Django users wrote:
> Does the test client execute javascript as well ? I can't remember.

No. Not even the HTML.

For that you need selenium, which is a whole different beast. The test client
is used for "unit tests for views". Selenium is in the realm of integration
tests, or acceptance tests if you will. It validates the end result, not a
unit of code.
--
Melvyn Sopacua

Bernd Wechner

unread,
Jun 12, 2018, 8:57:12 AM6/12/18
to Django users
Reply all
Reply to author
Forward
0 new messages