django 报错 ImproperlyConfigured at / Empty static prefix not permitted

write picture description here

The above picture is the prompt of the problem and the picture after the solution.

Reason: 
  The deployment method of django is quite special. It adopts the deployment method of static file path: STATICFILES_DIRS. The relative path and absolute path you wrote before are all invalid due to the lack of static file path...

The following is the solution process = " 
First of all, by default, you classify different folders css, js, image, etc. under static. static and templates are in the same directory.

||Step one:

/* Django's mapping mechanism, it is not important to fill in static or other English here*/

STATIC_URL = '/static/'
  • 1

/* When deploying static files (python manage.py collectstatic) all static static aggregation directories must be absolute addresses*/

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
  • 1

/* Set the path of static files such as pictures*/

STATICFILES_DIRS = (
    ('css', os.path.join(STATIC_ROOT, 'css').replace('\\', '/')),
    ('js', os.path.join(STATIC_ROOT, 'js').replace('\\', '/')),
    ('images', os.path.join(STATIC_ROOT, 'images').replace('\\', '/')),
    ('fonts', os.path.join(STATIC_ROOT, 'fonts').replace('\\', '/')),
)
  • 1
  • 2
  • 3
  • 4
  • 5

||Step two:

Add the following to the urls.py file:

...
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', VAapp.index),
    url(r'^index/$', VAapp.index),
    ...
]
urlpatterns += staticfiles_urlpatterns()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

||Step three:

Add in the first line of base.html

{% load staticfiles %}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325717910&siteId=291194637