Before you begin, you should have a Django project created and configured.
Install our library from PyPI, like so:
$ pip install django-bakery
Edit your settings.py and add the app to your INSTALLED_APPS list.
INSTALLED_APPS = ( # ... # other apps would be above this of course # ... 'bakery', )
Also in settings.py, add a build directory where the site will be built as flat files. This is where bakery will create the static version of your website that can be hosted elsewhere.
BUILD_DIR = '/home/you/code/your-site/build/'
The trickest step is to refactor your views to inherit our buildable class-based views. They are similar to Django’s generic class-based views, except extended to know how to automatically build themselves as flat files. Here is a list view and a detail view using our system.
If you’ve never seen class-based views before, you should study up in the Django docs because we don’t aren’t going to rewrite their documentation here. If you’ve already seen class-based views and decided you dislike them, you’re not alone but you’ll have to take our word that they’re worth the trouble in this case. You’ll see why soon enough.
from yourapp.models import DummyModel from bakery.views import BuildableDetailView, BuildableListView class DummyListView(BuildableListView): """ Generates a page that will feature a list linking to detail pages about each object in the queryset. """ queryset = DummyModel.live.all() class DummyDetailView(BuildableDetailView): """ Generates a separate HTML page for each object in the queryset. """ queryset = DummyModel.live.all()
After you’ve converted your views, add them to a list in settings.py where all buildable views will be collected.
BAKERY_VIEWS = ( 'yourapp.views.DummyListView', 'yourapp.views.DummyDetailView', )
Then run the management command that will bake them out.
$ python manage.py build
This will create a copy of every page that your views support in the BUILD_DIR. You can review its work by firing up the buildserver, which will locally host your flat files in the same way the Django’s runserver hosts your dynamic database-driven pages.
$ python manage.py buildserver
To publish the site on Amazon S3, all that’s necessary yet is to create a “bucket” inside Amazon’s service. You can go to aws.amazon.com/s3/ to set up an account. If you need some basic instructions you can find them here. Then set your bucket name in settings.py.
AWS_BUCKET_NAME = 'your-bucket'
Next, install s3cmd, a utility we’ll use to move files back and forth between your desktop and S3. In Ubuntu, that’s as simple as:
$ sudo apt-get install s3cmd
If you’re using Mac or Windows, you’ll need to download this file and follow the installation instructions you find there.
Once it’s installed, we need to configure s3cmd with your Amazon login credentials. Go to Amazon’s security credentials page and get your access key and secret access key. Then, from your terminal, run:
$ s3cmd --configure
Finally, now that everything is set up, publishing your files to S3 is as simple as:
$ python manage.py publish
Now you should be able to vist your bucket’s live URLs and see the site in action. To make your bucket act more like a normal website or connect it to a domain you control follow these instructions.
Getting started is as simple as returning to settings.py and adding the following:
BAKERY_GZIP = True
Then rebuilding and publishing your files.
$ python manage.py build $ python manage.py publish