Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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
Css Django静态文件链接_Css_Django_Django Staticfiles_Static Files - Fatal编程技术网

Css Django静态文件链接

Css Django静态文件链接,css,django,django-staticfiles,static-files,Css,Django,Django Staticfiles,Static Files,谢谢你调查这件事,因为我现在很紧张:) 我找不到style.css(或者任何静态文件),我在谷歌上搜索过,到目前为止没有一个解决方案有效 my URL.py: from django.conf.urls import patterns, include, url from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.conf import settings urlpatterns

谢谢你调查这件事,因为我现在很紧张:)

我找不到style.css(或者任何静态文件),我在谷歌上搜索过,到目前为止没有一个解决方案有效

my URL.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'),
)
我不明白我做错了什么(或者事实上为什么为这些静态数据提供服务会如此尴尬)。任何对湖人的帮助都将不胜感激

谢谢,
blargie bla

在您的服务器上,您可能需要尝试此功能

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

有关collectstatic命令的详细信息

也别忘了设置

以下是我的
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/
下,它与
静态文件夹不在同一级别上。

尝试:

{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}" type="text/css" />
{%load static%}

我的问题是在块内容之外添加css。将其移动到块内容中解决了问题

{% extends "home/base.html" %}
{% load static %}
<!--<link rel="stylesheet" href="{% static 'blog/main.css' %}">-->
{% block content %}
<link rel="stylesheet" href="{% static 'blog/main.css' %}">

     --some code--

{% endblock content%
{%extends“home/base.html”%}
{%load static%}
{%block content%}
--一些代码--
{%endblock内容%

这是在真正的服务器上,还是您只是在运行
/manage.py runserver
?您将扩展一个基本模板,然后将内容放置在块的外部。您可能希望在base.html中创建一个名为styles的块,然后在扩展模板中插入/包含样式表:``{%extends“home/base.html%”{%load static%}{%block style%}{endblock%}{%block content%}…{%endblock%}```
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}" type="text/css" />
{% extends "home/base.html" %}
{% load static %}
<!--<link rel="stylesheet" href="{% static 'blog/main.css' %}">-->
{% block content %}
<link rel="stylesheet" href="{% static 'blog/main.css' %}">

     --some code--

{% endblock content%