7 ms·
Using Django for mostly static sites
- skywhopper 13y agoImpressively long way around to avoid relying on Nginx or Apache. How does it perform in comparison?
- senko 13y agoThat's an excellent question (and an idea for a follow-up blog post). In most cases I'd go with Django+Gunicorn+Nginx setup (as described here http://goodcode.io/blog/django-nginx-gunicorn/ http://goodcode.io/blog/django-nginx-gunicorn/), with Nginx (or Apache) just being the reverse proxies and serving the static assets. Django is still needed here if you want to have form processing (unless you use 3rd party service), and useful if you want to have templating (unless you want to use Jekyll, Hyde, or similar static-generator tools). So, while Nginx/Apache would definitely speed up things, they're not enough. On the other hand, you can build a reasonably[0] fast "almost" static site with Django, and have it deployed on either your Linux server, or Heroku, or another PaaS. [0] What's "reasonably fast"? my hunch tells me that the simple setup outlined here could take quite a beating (unless intentionally attacked with slowloris or similar attacks, or serving heavy static assets). It'll be fun to test it.
- raverbashing 13y agoIt's much easier (deployment/maintenance) using Gunicorn/uwsgi feeding into Nginx/Apache than using a module within Nginx/Apache Especially with virtualenvs and such.
- sliverstorm 13y agoWhat's "reasonably fast"? I wish more people thought of http://forum.dlang.org/ http://forum.dlang.org/ as a reference example when they asked that question.
- mbell 13y agoYou can also setup a CloudFront deployment and point it to your app server as the origin. Fast and cheap way to serve static files without having to configure nginx/apache. Well...cheap as long as you don't need SSL.
- falcolas 13y agoOne nice thing with Django is that you can separate out truly static files into your apps static folder, and configure nginx to serve the /static/ URL. Configure a `STATIC_ROOT` for your app, run: ./manage.py collectstatic and you have nginx serving the truly static files while still allowing Django to manage your non-static assets. This is how I run my site, and it works pretty darned well. Let nginx do the caching of static files, compression, and SSL management, letting Django just worry about its small domain of non-static files. The one downside is that nginx will not necessairly detect when a static file has changed, requiring a bit of effort to ensure that new versions of a static file are puled, either through filename versioning, or `?1` on the end of your static URLs.
- bpicolo 13y agoThat's not unique to Django. You can always send static assets through the web server.
- falcolas 13y agoAbsolutely. But, this being an article on Django, serving a combination of static and semi-static assets, commenting on this Django feature seemed appropriate.
- StavrosK 13y agoIIRC, django-compressor will do that automatically.
- snide 13y agoIf you're looking to django for static mostly for the templating engine and some build time python, I'd check out the excellent Cactus. Apparently there's a new downloadable mac app for it, which I've yet to try, but as a Django templater / designer, Cactus was always my goto for static sites. https://github.com/koenbok/Cactus?source=c https://github.com/koenbok/Cactus?source=c
- jlafon 13y agoThe most interesting part to me is that there is no database. It had never occurred to me to use Django without one.
- motter 13y agoIt's surprisingly easy, though I believe the tests expect one to be configured (I haven't checked precisely why, or if it can be configured not to expect a database).
- StavrosK 13y agoI just configure it with SQLite in those cases. You can probably even specify ":memory:" for the DB location.
- mazelife 13y agoIt can be configured not to expect a database. You just subclass the DjangoTestSuiteRunner to skip setting up and tearing down the DB, then set that as TEST_RUNNER. See here for an example: http://stackoverflow.com/questions/5917587/django-unit-tests-without-a-db#answer-7004517 http://stackoverflow.com/questions/5917587/django-unit-tests...
- mcjiggerlog 13y agoAlso worth checking out is the Flask microframework. Setting up a simple static site with basic routing is ridiculously simple: http://flask.pocoo.org/docs/quickstart/ http://flask.pocoo.org/docs/quickstart/ I used it in conjunction with Bootstrap and Heroku to throw a personal site together in an evening, following this tutorial: http://www.shea.io/lightweight-python-apps-with-flask-twitter-bootstrap-and-heroku/ http://www.shea.io/lightweight-python-apps-with-flask-twitte...
- collyw 13y agoIts not that difficult to set up a Django site once you have done it a couple of times. What am I missing by not knowing Flask?
- emidln 13y agoFlask doesn't make as many decisions for you as Django does. Some things that come bundled in Django are packages in Flask. A lot of the packages for Flask make fewer assumptions than similar packages for Django, which often makes using an uncommon data backend (ElasticSearch, Mongo, RethinkDB, Redis, etc) a little easier (you don't have to rewrite as much).
- mbesto 13y agoFlask (for python) and Sinatra[0] (for Ruby) are awesome. I just recently did two side projects using both. I use Webaction[1] for my hosting provider for "small" projects and getting Flask/Sinatra requires very little setup (just enough to make it fun). They also have one click installers for Rails/Django/etc. Super easy to setup if you're a dev noob like me. [0] - http://www.sinatrarb.com/ http://www.sinatrarb.com/ [1] - http://www.webfaction.com/?affiliate=mbesto http://www.webfaction.com/?affiliate=mbesto
- eli 13y agoHave you seen Frozen-Flask? It's a clever extension that "freezes" a dynamic flask site into HTML files and static assets you can upload anywhere. http://pythonhosted.org/Frozen-Flask/ http://pythonhosted.org/Frozen-Flask/
- motter 13y agoI'm in the process of moving away from a static site for my website. Simply put, a web interface is often more convenient so I can update things easier on the go, and it's good to have a place to host experiments too. I have used various static site generators but none of them seemed to be significantly less work to get going than a small django app on heroku. Though I've been using Django for a few years, so there is simply no learning curve left.
- untog 13y agoWait till your web site gets a ton of unexpected traffic.
- StavrosK 13y agoWhat'll happen then? My Django-based blog handled hundreds of thousands of visits in a day without breaking a sweat. What it does is basically "fetch the whole page from memcached and serve it".
- riffraff 13y agoHaving a dynamic "admin panel" does not mean you can't completely cache your user-facing pages.
- oneeyedpigeon 13y agoAnd having HTML files on the filesystem doesn't mean you can't have an admin panel to update them with.
- milkanic 13y agoI'd recommended checking out Mezzanine[1]. I use it for everything now, regardless if I need a full CMS. The included fab file makes deployment dead simple[2] and the caching strategy[3] results in nearly zero DB hits. [1]http://mezzanine.jupo.org/ http://mezzanine.jupo.org/ [2]http://mezzanine.jupo.org/docs/deployment.html http://mezzanine.jupo.org/docs/deployment.html [3]http://mezzanine.jupo.org/docs/caching-strategy.html http://mezzanine.jupo.org/docs/caching-strategy.html
- natrius 13y agoDon't do this. If you aren't using a database or processing forms, your life will be better if you learn jekyll and use it.
- StavrosK 13y agoUntil you need to add a bit of dynamicity, such as a contact form or something trivial, sadly.
- natrius 13y agoThere are plenty of form services you can drop on a static page. That is what you should do.
- StavrosK 13y agoThat assumes the only thing you'll need is a contact form, though. My site has various things, such as a page that shows a graph of my daily weight.
- natrius 13y agoSo... you're using a database? Great. Use Django.
- avalaunch 13y agoI think his point is that it's quite likely you'll begin without a need for a database but later want to add some functionality requiring one. And at that point, you will have been better off starting with Django (or similar) as now you'll need to migrate everything over anyway.
- natrius 13y agoThat point is wrong. Setting up a Django deployment that won't fall over when you get traffic is more work than moving Jekyll templates into a Django install. Plus, if he'd started with Jekyll, he might have chosen to store his weights as a flat file that Jekyll could use to generate static charts. Database-first thinking for content sites is just wrong. I've done it based on this same argument, and I've learned my lesson.
- lumpypua 13y agoI built a decently large static content site with a dynamic backend (http://yareallyarchive.com http://yareallyarchive.com) on django. With aggressive caching of all static pages it is fast as hell, and I love the django ecosystem. Adding search was as easy as dropping in django-haystack and adding like 5 lines of code. django-mptt has been brilliant for easily querying and manipulating comment trees. It's possible to do small sites with django but it requires you to learn a lot of stuff you don't necessarily need. Flask is a better choice for small/nearly-static sites if you're not already familiar with a web framework.
- koenbok 13y agoWe just shipped a Mac app on top of Cactus, an open source static site generator based on django. http://cactusformac.com http://cactusformac.com
- dangayle 13y agoThis is exactly the method my company uses when one of the stories on our website is getting hammered by Reddit. I manually wget the rendered template, save it to a templates/static/ dir, and serve it directly. Works like a charm.
- leephillips 13y agoHave you tried using django's built-in caching support?
- dangayle 13y agoWe cache everything, but this simply takes care of not having to think about it. It might not be the best method, but it sure is the least troublesome/least cognitive load when things hit the fan.
- amitu 13y agoOr you can use djangothis[1]. $ pip install djangothis $ cat > views.py from importd import d @d def hello(request): return d.HttpResponse("hello there") ^D $ djangothis Validating models... 0 errors found February 18, 2014 - 16:43:31 Django version 1.6.2, using settings None Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. Any html files would be automatically served, files in static folder in current folder will automatically be mapped to /static/ and so on. [1]: https://github.com/amitu/djangothis https://github.com/amitu/djangothis