Support

Find answers, guides, and tutorials to supercharge your content delivery.

Django CDN Integration

Updated on October 4, 2018
Django CDN Integration

This Django CDN integration guide will walk you through how to set up Django CMS with KeyCDN to deliver your static assets. Django CMS is an open source, Python-based content management system that allows you to quickly set up an application and easily scale it as it gains more traction. The user friendly interface allows content editors to easily modify the look and feel of the site while developers can seamlessly integrate and extend upon the existing application.

How to complete a Django CDN integration

  1. First create a Pull Zone within your KeyCDN dashboard.

  2. Configure your urls.py file to point the static files to the appropriate static folder (e.g. same directory as your media folder)

    Before:

    # This is only needed when using runserver.
    if settings.DEBUG:
        urlpatterns = patterns('',
            url(r'^media/(?P<path>.*)$', 'django.views.static.serve', # NOQA
                {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
            ) + staticfiles_urlpatterns() + urlpatterns # NOQA
    #urlpatterns = static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    

    After:

    # This is only needed when using runserver.
    if settings.DEBUG:
        urlpatterns = patterns('',
            url(r'^static/(?P<path>.*)$', 'django.views.static.serve', # NOQA
                {'document_root': settings.STATIC_ROOT, 'show_indexes': True}),
            ) + urlpatterns # NOQA
    urlpatterns = patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', # NOQA
            {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
        ) + staticfiles_urlpatterns() + urlpatterns # NOQA
    #urlpatterns = static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    
  3. Modify the settings.py file to point your Django application to the CDN. Find the STATIC_URL and MEDIA_URL variables and modify them to reflect your Zone URL or Zone Alias similar to:

    STATIC_URL = 'http://keycdndjango-hexid.kxcdn.com/static/'
    MEDIA_URL = 'http://keycdndjango-hexid.kxcdn.com/media/'
    

    (Replace http://keycdndjango-hexid.kxcdn.com with your Zone URL or Zone Alias)

  4. At this point, any files within the static or media folders should be delivered through the CDN. However, there are still miscellaneous static files that Django uses to run the CMS. In order to complete the integration, these files must be moved to your STATIC_ROOT folder, defined in the settings.py file. In order to copy these static files, first ensure you are running in a virtual environment from the Django root directory:

    $ source env/bin/activate
    

    Once this is complete, navigate to your app directory, where the manage.py file is stored, and run the following command:

    $ python manage.py collectstatic
    

    Any external static files will now be pulled into your STATIC_ROOT folder which will subsequently be delivered through the CDN.

  5. Use the STATIC_URL within your template files in order to reference your files from the static folder, for example:

    <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}test.css">
    
  6. Double check your page source to ensure that your Django CDN integration was successful and the media/static files are indeed being delivered through the CDN.

Benefits of using a CDN with Django

Now that your Django CDN integration is done, your website will receive a boost in performance and a reduction in latency. There exists many benefits to users who accelerate their Django project with KeyCDN:

  • Access to our complete network of POPs for greater global availability and speed
  • HTTP/2 support for reduced latency and optimized content delivery
  • Low pricing that decreases with volume
  • Let's Encrypt support that allows you to establish an SSL secured connection between our edge servers to your users, free of charge

KeyCDN also offers many other features and advantages to users who implement a Django CDN integration to accelerate their site.

Supercharge your content delivery 🚀

Try KeyCDN with a free 14 day trial, no credit card required.

Get started
KeyCDN uses cookies to make its website easier to use. Learn more