这是我的网站目录:
django_project
\bin
\include
\lib
\src
\django_project
settings.py
\app2
manage.py
\static
\js
\css
\media
\templates
base.html
我添加到settings.py的内容是:
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "templates"),
)
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static"),
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media"),
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static"),
),
和我的urls.py:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
嘿,它不起作用。我在base.html中引用它们的方式是:
<link href="/static/css/bootstrap.min.css" rel="stylesheet">
有什么想法吗?
谢谢。 哈桑
答案 0 :(得分:1)
运行您已粘贴的设置会给我一个错误ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
。在设置中注释掉os.path.join(os.path.dirname(BASE_DIR), "static"),
删除了错误,模板正确找到了没有URLs.py修改的CSS文件。
答案 1 :(得分:1)
首先,正如@r --------- k所提到的,除了元组之外,我不应该使用尾随逗号。所以settings.py应该如下所示:
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "static")
),
第二件事是,正如您可能注意到的那样,STATIC_ROOT和STATICFILES_DIRS不可能具有相同的值。我在静态文件夹中添加了另一个静态文件夹,并将我的js和css文件夹放在其中:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "static")
),
目录如下:
django_project
\bin
\include
\lib
\src
\django_project
settings.py
\app2
manage.py
\static
\static
\js
\css
\media
\templates
base.html
最后,您应该在urls.py中使用urlpatter:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
答案 2 :(得分:0)
这似乎在您的using(var httpResponse = (HttpWebResponse)request.GetResponse())
{
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
var responseStream = httpResponse.GetResponseStream();
if (responseStream != null)
{
// Line reached only if httpResponse.GetResponseStream() isn't null
using (var streamReader = new StreamReader(responseStream))
{
var result = streamReader.ReadToEnd();
...
}
}
}
else
{
...
}
}
中的当前django(2.2)版本中有效:
urls.py
记住要运行from django.conf import settings
from django.conf.urls.static import serve
from django.urls import include, path
urlpatterns += [
path(settings.STATIC_URL[1:], serve, {'document_root': settings.STATIC_ROOT })
]
将静态文件收集到./manage.py collectstatic