Kyle Swanson Kyle Swanson - 29 days ago 13
Python Question

Serving static files from root of Django development server

My goal is to have an Angular project being served from the root of my development server. The files will be completely static as far as Django is concerned, no Django template processing is needed. The angular project will then make resource calls to a Django project located at /api/ on the same development server, which will then return json results generated from a view for the Angular project to process.

I assumed it would be as easy as adding the following to my urls.py file.

url(r'^/', 'django.views.static.serve', {
'document_root':'/Users/kyle/Development/project/site/app',
}),


Or

+ static("^/$", document_root="/Users/kyle/Development/project/site/app")


To the end of the urlpatterns.

With /project/site/app being the directory with the Angularjs files.

However, both of these leave me with 404 errors.

I'm open to changing the structure of the project if a more obvious solution exists.

Answer

It turned out that it was a combination of 2 things, as shavenwarthog said, it shouldn't have the slash. Also, it needed a regular expression to direct it to the file. The final line ended up being:

url(r'^(?P<path>.*)$', 'django.views.static.serve', {
        'document_root':'/Users/kyle/Development/project/site/app',
}),

I can then access files like

http://localhost/beer.jpg