Serve Static Pages from S3 using Django - python

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.

Related

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!

How to make almost static site in Pyramid?

I'm switching to Pyramid from Apache/PHP/Smarty/Dreamweaver scheme.
I mean the situation of having static site in Apache with menu realized via Dreamweaver template or other static tools. And then if I wanted to put some dynamic content in html I could make the following:
Put smarty templates in html.
Create php behind html with same name. Php takes html as template.
Change links from html to php.
And that was all.
This scheme is convenient because the site is viewable in browser and editable
in Dreamweaver.
How can I reproduce this scheme in Pyramid?
There are separate dirs for templates and static content. Plus all this myapp:static modifiers in hrefs. Where to look up?
Thank you for your advices.
There is no smarty port for Python. So you would have to start using another template syntax, such as mako or chameleon
To do this, you would setup your view_config to respond to the url, end tell it to use the corresponding template.
If you want to do this, you would simple change your code. But this is not necessary, pyramid will process your requests, whether the url contains .html, .php, .python, /, or whatever.
You could still edit the templates in Dreamweaver I guess.
Only really static pages would be linked using static_url. If it is html that you mean to make into a template, it might be easiest to just start of with a template right away, without any dynamic content in it.
This is from the URL dispatch tutorial:
# in views.py
#view_config(route_name='view_page', renderer='templates/view.pt')
def view_page(request):
return {}
# in __init__.py
config.add_route('view_page', 'mypage.html')
You can build a small web application which uses traversal to serve html documents from a directory. Here's more explanations about how traversal works.
Then you can programmatically render those documents as Chameleon templates, using PageTemplateFile for example. This would allow you to include, say, common header/footer/navigation into every page.
This would mean that every page in your site will be in fact dynamic, so that would incur a small performance penalty for every page regardless of whether it has dynamic content or not, but you should not be concerned with this unless you're building the next Facebook. :) However, this approach would allow you to have a plain html document corresponding to every page in your website which you'll be able to edit using Dreamweaver or any other editor.
This is somewhat a different answer than ohters but here is a completely different flow.
Write all your pages in html. Everything!!! and then use something like angularjs or knockoutjs to add dynamic content. Pyramid will serve dynamic content requested using ajax.
You can then map everything to you html templates... edit those templates wherever you want since they are simply html files.
The downside is that making it work altogether isn't that simple at first.

How to convert Django dynamic page to static HTML file?

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

Categories