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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Bernd Wechner
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/6353b82e-7f0d-465f-9eba-68ed207940a8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Melvyn Sopacua

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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/3641770.R8fxcq3Jsz%40fritzbook.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Bernd Wechner
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 <a href="https://docs.djangoproject.com/en/2.0/topics/testing/tools/#the-test-client" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2F2.0%2Ftopics%2Ftesting%2Ftools%2F%23the-test-client\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGtg2T7T3gxpx1EzR71I_mwFURqRQ&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2F2.0%2Ftopics%2Ftesting%2Ftools%2F%23the-test-client\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGtg2T7T3gxpx1EzR71I_mwFURqRQ&#39;;return true;">test client ... if your template uses request related information (like logged in user). The cheaper method is <a href="https://github.com/melvyn-sopacua/django_xtc/blob/master/xtc/__init__.py#L49" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fmelvyn-sopacua%2Fdjango_xtc%2Fblob%2Fmaster%2Fxtc%2F__init__.py%23L49\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHp_fxa6mEqGoagGm7RBAWl5ZpETw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fmelvyn-sopacua%2Fdjango_xtc%2Fblob%2Fmaster%2Fxtc%2F__init__.py%23L49\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHp_fxa6mEqGoagGm7RBAWl5ZpETw&#39;;return true;">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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Anthony Flury
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 [hidden email]
> <mailto:[hidden email]>.
> To post to this group, send email to [hidden email]
> <mailto:[hidden email]>.
> 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 : *[hidden email]*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/03b51b49-d1b8-b7f4-9ff4-5d8d8ff3b54a%40btinternet.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Melvyn Sopacua
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/2061765.he2AIJnB6B%40fritzbook.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Bernd Wechner
In reply to this post by Bernd Wechner


On Monday, 4 June 2018 16:00:09 UTC+10, 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.

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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/81c34e1c-71af-4a43-9e3b-088b3b1383fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.