Urlpatterns = [...] + static (settings.STATIC_URL) get escape character

When I add STATIC_URL to urls.py:

urlpatterns = [...]+static(settings.STATIC_URL)

but there I get ^static\/(?P<path>.*)$in the urls.

Shouldn't it be ^static/(?P<path>.*)$? how ^media/(?P<path>.*)$.

enter image description here


in settings.py:

STATIC_URL = '/static/'

STATIC_ROOT = BASE_DIR + '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

How to solve this problem? or is there another way to replace

+static(settings.STATIC_URL)

if there is, provide me for testing, thanks.

+4
source share
2 answers

There is a way to avoid this problem. in urls.py:

from django.conf.urls.static import serve

if settings.DEBUG:
    urlpatterns += [
        url(r'^static/(?P<path>.*)$', serve, {
            'document_root': settings.STATIC_ROOT
        })
    ] 

The result will be like this:

^media/(?P<path>.*)$
^static/(?P<path>.*)$  # this is as the same with the media
0
source

Static is used to direct a static URL in local server mode. Unfortunately, static(settings.STATIC_URL)it seems a little broken.

This seems to work in the current django (2.2):

from django.conf.urls.static import serve

urlpatterns += [
    path(settings.STATIC_URL[1:], serve, {'document_root': settings.STATIC_ROOT })
]

After starting, the ./manage.py collectstaticlocal server will properly serve all static files. Including django_debug.

0
source

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


All Articles