Serving static files from the root of a Django development server

My goal is to create an Angular project from the root of my development server. Files will be completely static with respect to Django, processing of Django templates is not required. The Angular project then calls the resource calls to the Django project located in / api / on the same development server, which then returns the json results created from the view for the Angular project for processing.

I suggested that it would be easy to add the following to the 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 urls.

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

However, both of them leave me 404 errors.

I can change the structure of the project if there is a more obvious solution.

+4
3
  • , Django . 404, /Users/kyle/Development/project/site/app/beer.jpg http://localhost/beer.jpg?

  • urls.py URL- ; url(r'beer') url(r'^/beer')

STATIC. , (.. Nginx) :

https://docs.djangoproject.com/en/dev/howto/static-files/

+1

, , shavenwarthog, . , . :

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

http://localhost/beer.jpg
+2

You need to serve both index.html and your static files, which are executed in Django 1.10 as follows:

from django.contrib.staticfiles.views import serve
from django.views.generic import RedirectView

urlpatterns = [

    # / routes to index.html
    url(r'^$', serve,
        kwargs={'path': 'index.html'}),

    # static files (*.css, *.js, *.jpg etc.) served on /
    url(r'^(?!/static/.*)(?P<path>.*\..*)$',
        RedirectView.as_view(url='/static/%(path)s')),
]

See this answer where I wrote a more complete explanation of this configuration, especially if you want to use it for production.

+2
source

Source: https://habr.com/ru/post/1542532/


All Articles