2017-02-09 125 views
0

我完全迷失了,无法弄清楚如何配置路径到一个css文件。 网站结构是:Django中静态文件的路径 - 404

--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. 
+0

怎么样'STATICFILES_DIRS'变量? – arcegk

+0

我不使用它,因为我不知道该放什么。你做? – mimic

回答

0

尝试将其更改为:

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

感谢您的回复。如果我改变了STATIC_URL,实际的URL变成只是http://127.0.0.1:8000/static/styles/site.css,仍然是404,因为现在地址是错误的。 :( – mimic

1

这终于为我工作:

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

好吧,看起来只有斜线丢失了。我在想静态根也是不正确的。 –