Django中静态文件的路径 - 404

时间:2017-02-09 02:36:15

标签: django django-staticfiles

我完全迷失了,无法弄清楚如何配置只有一个css文件的路径。 网站结构是:

--mysite
 |
 |-/static
    | 
    /styles
        |
       site.css

我应该在STATIC_URL和STATIC_ROOT中添加什么才能使其正常工作? 以及如何链接这个文件? (目前它已经

<link rel='stylesheet' href="{% static 'styles/site.css' %}" />

) 和

STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'mysite/static/')

和mysite.urls:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^hello/$', hello),
    url(r'^mysite/$', mysite)
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

错误是:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/mysite/static/styles/site.css
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^hello/$
^mysite/$
^static\/styles\/(?P<path>.*)$
The current URL, mysite/static/styles/site.css, didn't match any of these.

2 个答案:

答案 0 :(得分:1)

这最终对我有用:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'mysite/static')

答案 1 :(得分:0)

尝试将其更改为:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'mysite/')