Skip to content

Commit

Permalink
Add CORS headers to dev server media.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Kelly committed Jan 6, 2015
1 parent b6868f7 commit fd56349
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion snippets/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.http import HttpResponse
from django.views.static import serve as static_serve

from funfactory.monkeypatches import patch

Expand Down Expand Up @@ -29,8 +30,14 @@ def robots_txt(request):

## In DEBUG mode, serve media files through Django.
if settings.DEBUG:
# Use custom serve function that adds necessary headers.
def serve_media(*args, **kwargs):
response = static_serve(*args, **kwargs)
response['Access-Control-Allow-Origin'] = '*'
return response

urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
url(r'^media/(?P<path>.*)$', serve_media, {
'document_root': settings.MEDIA_ROOT,
}),
) + staticfiles_urlpatterns()

0 comments on commit fd56349

Please sign in to comment.