css - Django 静态文件链接

标签 css django django-staticfiles static-files

感谢您调查此事,因为它现在让我很紧张:)

我无法获得 style.css(或任何静态文件!),我用 google 搜索了一下,到目前为止没有一个解决方案有效。

我的 urls.py:

 from django.conf.urls import patterns, include, url
 from django.contrib.staticfiles.urls import staticfiles_urlpatterns
 from django.conf import settings

 urlpatterns = patterns('',
   url(r'^$', 'ports.views.home', name='home'),
 )
 urlpatterns += staticfiles_urlpatterns()

 if settings.DEBUG:
    urlpatterns += patterns('django.contrib.staticfiles.views',
    url(r'^static/(?P<path>.*)$', 'serve'),
)

我的 base.html,我在其中加载静态数据:

{% load staticfiles %}

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="static/css/style.css" type="text/css" />
...

我的 settings.py 引用静态目录:

STATIC_URL = '/static/'

我不明白我做错了什么(或者实际上为什么服务器这些静态数据必须如此尴尬)。任何湖人方面的帮助,我们将不胜感激!

谢谢, 布拉吉布拉

最佳答案

在你的服务器上,你可能需要试试这个

python manage.py collectstatic -l

-l创建符号链接(symbolic link)而不是复制。这可以防止同一文件的两个副本并节省一些空间,但也意味着如果您重命名原始文件,则必须重新建立链接。

有关 collectstatic 命令的更多信息 here .

不要忘记设置 STATIC_ROOT

这里仅供引用是我的settings.py对于那部分:

# 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(os.path.dirname(__file__), '..', 'static').replace('\\', '/')

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

这适用于 Django 1.4+,其中 settings.py在(例如)mysite/mysite/ 下而且它与 static 不在同一级别文件夹。

关于css - Django 静态文件链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15989416/

相关文章:

python - Django 1.7静态文件目录扫描

python - Django 静态根 404 错误

django - this.$apollo 始终未定义

python - 无效的 HTTP_HOST header : '0.0.0.0:8000' . 您可能需要将 '0.0.0.0' 添加到 ALLOWED_HOSTS

html - 如何使用 Bootstrap(和 React)居中图像

css - 如何在 bootstrap 3+ 中将图像置于 col 元素的中心

Django - 从缓存的查询中填充模型实例相关字段

python - 如何在 Django Admin 中覆盖 css?

html - 制作一个 logo + nav + social 的导航栏

html - 为什么 CSS 文件没有加载到 React 应用程序中