Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django静态文件-CSS不工作_Python_Django_Django Urls_Static Files - Fatal编程技术网

Python Django静态文件-CSS不工作

Python Django静态文件-CSS不工作,python,django,django-urls,static-files,Python,Django,Django Urls,Static Files,可能重复: 在base.html中加载CSS有一个问题。我将所有css文件放在/static目录下 在urls.py中,我输入以下代码: if settings.DEBUG: urlpatterns += patterns('', (r'^static/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': '/home/bkcherry/botstore/bots

可能重复:

在base.html中加载CSS有一个问题。我将所有css文件放在/static目录下

urls.py
中,我输入以下代码:

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve',
           { 'document_root': '/home/bkcherry/botstore/botstore/static' }),
    )
if settings.DEBUG:
urlpatterns+=模式(“”,
(r'^static/(?P.*)$,'django.views.static.service',
{'document_root':'/home/bkcherry/botstore/botstore/static'}),
)
在base.html中,我添加了以下内容:

<link rel="Stylesheet" type="text/css" href="/static/css.css" />


当我转到main.html时,css样式不起作用。我需要配置settings.py
MEDIA\u ROOT
MEDIA\u URL
STATIC\u ROOT

如果您查看官方文档,我想您需要在路径的末尾加一个斜杠,即
'/home/bkcherry/botstore/botstore/STATIC/'

from django.conf import settings

# ... the rest of your URLconf goes here ...

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
    )
来自django.conf导入设置的

# ... 你的URLconf的其余部分都在这里。。。
如果设置为.DEBUG:
urlpatterns+=模式(“”,
url(r'^media/(?P.*)$,'django.views.static.service'{
“document\u root”:settings.MEDIA\u root,
}),
)

MEDIA\u ROOT应该有/在末尾()

您必须不要使用MEDIA\u ROOT或MEDIA\u URL这是用于上传的媒体而不是静态内容,并且您不需要设置URL模式,因为这仅适用于django 1.2或“如果您使用其他服务器进行本地开发”:

您需要将静态文件放入: botstore/botstore/static/botstore/css.css

然后使用:

HOME_ROOT = os.path.dirname(__file__)

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"

STATIC_ROOT = os.path.join(HOME_ROOT, 'staticfiles')

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
然后在HTML中,您可以引用静态文件,因此:

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}botstore/css.css" />


如果您将浏览器指向会发生什么情况?这也会有所帮助。也不要错过传递RequestContext。好了,现在开始工作了!!;)我修复了urls.py:if settings.DEBUG:urlpatterns+=patterns(“”,url(r'^static/(?P.*)$,'django.views.static.service',{'document_root':settings.MEDIA_root,})上的代码然后我配置settings.py:MEDIA_ROOT='/home/bkcherry/botstore/botstore/static/'MEDIA_URL='/static/'static_ROOT=''static_URL='',现在该方法找到了目录。我在MEDIA_ROOT的末尾放了斜杠,现在开始工作!!;)