How to convert Django dynamic page to static HTML file? - python

The current website is running on Django, Gunicorn and nginx. I want a way to convert the current front page into a static HTML page and want nginx to serve this static page instead of going through the whole web stack. I want the front page to load faster. This can be done manually, but is there a tool integrated with Django or Gunicorn that automatically convert certain page into static and serve those pages?

put it in your /media/ folder
then just point to
some.url/media/html/some_static_html.html

Related

Django serving static pages from Sphinx

I have a large group of static html pages that I've generated from sphinx, and I'd like to show them on my Django site. I can connect to at least one page by putting the html/ folder form sphinx in a templates directory inside my app, and changing the urls.py file to include
url(r'^$', TemplateView.as_view(template_name="index.html")
Then it finds the index.html file inside myapp/templates/html. However, none of the internal links work (it'll try to redirect through Django and give me a 404 error). Also, the static files won't load in (Sphinx generates a _static folder, and even though I put that in the myapp directory, and the Chrome network tab tells me it's trying to load the css from myapp/_static, still nothing).
Is there any way to make all the links relative to each other inside this project? Alternatively, can I get Django to just serve up the whole project as static pages?
It looks like FlatPages is almost what I'm looking for, but I have more than just a title and content in these pages.
Firstly, you should not be using Django to serve essentially static files. This is what your webserver is for.
If you are using Apache, you can use the <directory> directive to serve these files alongside your Django routes.

How can I added some sub html project to a django project

Now I have a django project and some small html project (in fact, some html5 games). How can I added the html site to the django project?
For the each html project has ref a lot of css, img and javascript in its own folder, and different html site are in different folder.
Hence, I can not now just do like this:
t = get_template('htmlprojectfolder/index.html')
html = t.render(Context())
return HttpResponse(html)
I think my problem is:
"how can I jump to a html index page with its static files correct linked?" (Clealy, I can not read just the index page as a template file.)
Thank you for help!
I think the best way to add static, non-Django files that shall be accessible via a Django project would be to add the files to the STATICFILES_DIR. You can read more about static files here in the Django documentation.
Another, and possibly better, way to handle this would be to let the webserver serve the static files separately from Django.

Django: dynamically sharing static files

I am working on a project in django 1.4.4, what i am trying to achieve is i have website's index pages mapped with a unique url in database. whenever url hits that url, it should load that website.
I am able to load the index.html(easy) using http://lclhst.com/landing/one-bush
But the problem is the static files that web page auto loads.
so what i have right now is (website) in media/uploads/{unique-no}/index.html and other media files. I tried to make an all url accepting view but i think django auto-tries to serve media files when requested so all the hits like http://lclhst.com/landing/one-bush/_include/images/dot.jpg are being 404'd.
i am looking for any mistake in my approach or some better approach.

pylons route with period

I am currently trying to make a once static page into a dynamic page. The customers does not want to change the url to not have the .html at the end of the url. So, as an example the current static page is /foo/bar.html which is located in my public folder, no problem. I can easily make it /foo/bar, but once I have a period pylons no longer excepts the route.
current code:map.connect('foo', '/foo/bar.html',controller=controller , action='foo')
I just figured out that all I needed to do was add {.format} and rename the original file because pylons with route to the static page first!

Serve Static Pages from S3 using Django

I'm planning to build a Django app to generate and later server static pages (probably stored on S3). When users visit a url like mysite.com/static-pages/12345 the static file in my S3 bucket named 12345.html should be served. That static file might be the static html page of a blog page my site has generated for the user, for example.
This is different from including static resources like CSS/Javascript files on a page that is rendered as a Django template since I already know how to use Django templates and SQL databases - what's unfamiliar to me is that my "data" is now a file on S3 rather than an entry in a database AND that I don't actually need to use a template.
How exactly can I retrieve the requested data (i.e. a static page) and return it to the user? I'd like to minimize performance penalties within reason, although of course it would be fastest if users directly requested their static pages from S3 (I don't want them to do this)".
A few additional questions:
I've read elsewhere about a django flatpages app which stores html pages in a database, but it seems like static html pages are best stored on a filesystem like S3, no?
Is there a way to have the request come in to my Django application and have S3 serve the file directly while making it appear to have come from my application (i.e. the browser url still says mysite.com/static-pages/12345, but the page did not go through my Django server)?
Thanks very much!
Amazon S3 doesn't support URL rewriting (it's not a webserver), so you're going to have no choice but to proxy the requests to a web server or service that can rewrite the urls for you.
You could use a web server you control and follow the instructions here to have apache rewrite the URLs, but that seems somewhat wasteful when the whole point is loading a static website.
Alternatively, I have a solution that might work if you want to stay purely in S3:
You have the option to specify an HTML document that will be returned to the user's browser in the case of 404 - the error document. You could create a tiny HTML page that checked to the current URL, and simply changed window.location to go to "rewritten" url without the .html extension:
<html>
<script>
var slash = window.location.lastIndexOf("/");
var dot = window.location.lastIndexOf(".");
if (slash < dot) && (dot != -1) {
window.location = window.location + ".html";
}
</script>
</html>
Obviously you'd want to make it more robust, but you get the idea.
The downside is that each request for your static pages using the url will make an extra round-trip from the user's browser to your server (once for the 404 page, and then once to get the real page).
Also, you'd need to adjust my above code to avoid triggering a 404 loop in the case of an actual url being incorrect by adding a check something like:
var loopcheck = window.location.IndexOf(".html");
if (lookcheck != -1) {
window.location = "real404.html";
return;
}
Hope that helps.
You can just create index.html inside /static-pages/12345/ folder and it will be served.

Categories