Crushing, caching and CDN deployment in Django

├── compressed │   ├── __init__.py │   ├── settings.py │   ├── urls.py │   └── wsgi.py ├── coupon │   ├── admin.py │   ├── __init__.py │   ├── migrations │   │   └── __init__.py │   ├── models.py │   ├── tests.py │   └── views.py ├── db.sqlite3 ├── external │   ├── css │   │   ├── app.css │   │   ├── bootstrap.css │   │   ├── bootstrap.css.map │   │   ├── bootstrap.min.css │   │   ├── bootstrap-theme.css │   │   ├── bootstrap-theme.css.map │   │   └── bootstrap-theme.min.css │   ├── fonts │   │   ├── glyphicons-halflings-regular.eot │   │   ├── glyphicons-halflings-regular.svg │   │   ├── glyphicons-halflings-regular.ttf │   │   └── glyphicons-halflings-regular.woff │   ├── img │   │   └── mark.jpg │   └── js │   ├── app.js │   ├── bootstrap.js │   ├── bootstrap.min.js │   └── npm.js ├── manage.py ├── static └── templates └── base.html The codebase In compressed/urls.py I mapped all URLs to a single view and added in a static content endpoint for times when the code is run in debug mode..When running on production Ill change STATIC_URL to a CDN endpoint stub..from django.conf import settings from django.conf.urls import patterns, include, url from django.conf.urls.static import static from coupon import views urlpatterns = patterns('', url(r'^$', views.home, name='index'), ) if settings.DEBUG: urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) I created a unit test for the single view..I didnt patch the cache file generation as Ill use the test to generate the cache files on a continuous integration server..With those files generated, Ill deploy them to a CDN..A cleaner way to approach this would be to patch the unit tests so they dont create file artefacts and use a management command to generate the static cache files..coupon/tests.py: from django.core.urlresolvers import reverse from django.test import TestCase from django.test.client import Client class ViewTest(TestCase): def setUp(self): self.client = Client() def test_index(self): resp = self.client.get(reverse('index')) self.assertEqual(resp.status_code, 200) The single view in coupon/views.py renders a template and returns it..from django.shortcuts import render def home(request): return render(request, 'base.html') I created a templates/base.html file which is mostly an example file from the bootstrap project..I added some template tags for identifying sections of markup to minify and wrapped asset URLs with a static template tag so their path prefix can be controlled via settings.STATIC_URL..{% load staticfiles %} {% load compress %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap 101 Template</title> {% compress css %} <link href="{% static "css/bootstrap.min.css" %}" rel="stylesheet"> <link href="{% static "css/app.css" %}" rel="stylesheet"> {% endcompress %} <!–[if lt IE 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]–> </head> <body> <h1>Hello, world!</h1> <img src="{% static "img/mark.jpg" %}" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> {% compress js %} <script src="{% static "js/bootstrap.min.js" %}"></script> <script src="{% static "js/app.js" %}"></script> {% endcompress %} </body> </html> The compressed/settings.py file is the longest and probably most complicated of the project..Here is a summary of the key settings being put in place: Fetch the SECRET_KEY environment variable..Add compressor to the installed apps tuple.. More details

Leave a Reply