【问题标题】:serving static files django development提供静态文件 django 开发
【发布时间】:2014-03-13 00:14:47
【问题描述】:

我知道有一百万人问过这个问题,我读了又读,但仍然无法让它发挥作用(我认为这说明文档咳嗽咳咳)。

一个。 CSS: 这是我的设置:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'))
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static")
STATIC_ROOT = '/static/'
STATIC_URL = '/static/'

也相关:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'south',
    'blog',
)

我的模板可以正常使用,而我的 css 却不行。它位于 static/originaltheme.css

我都试过了:

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

和

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

两个。另外,有没有办法让硬编码的网址显示出来?例如,假设博客文章的内容有一个硬编码到 /static/img1.jpg 的 url,我该怎么做?所有的文档都指向 {{ static_url }},但我猜测从数据库的文本字段返回时不会得到评估...

我的网址有什么关系吗?

from django.conf.urls import patterns, include, url

from django.contrib import admin
from django.conf.urls.static import static
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'nickswebsite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^$', 'blog.views.index'),

    url(r'^blog/view/(?P<slug>[^\.]+)',
    'blog.views.view_post',
    name='view_blog_post'),

    url(r'^blog/category/(?P<slug>[^\.]+)',
    'blog.views.view_category',
    name='view_blog_category'),

    url(r'^admin/', include(admin.site.urls)),
)

编辑:

我试过这样做是在网址中:

urlpatterns = patterns('',
    url(r'^$', 'blog.views.index'),

    url(r'^blog/view/(?P<slug>[^\.]+)',
    'blog.views.view_post',
    name='view_blog_post'),

    url(r'^blog/category/(?P<slug>[^\.]+)',
    'blog.views.view_category',
    name='view_blog_category'),

    url(r'^admin/', include(admin.site.urls)),
) + static(settings.STATIC_URL, document_root=setting.STATIC_ROOT)

urlpatterns += staticfiles_urlpatterns()

但我得到一个“设置”未定义的错误。我尝试导入设置,但仍然出现错误。


编辑

好的,在开发服务器上提供静态服务的文档很糟糕。写它的人真的应该重写它。这家伙是全世界唯一一个似乎解释清楚的人:http://agiliq.com/blog/2013/03/serving-static-files-in-django/#servee

你在 settings.py 中确实需要这些(为什么它们没有包含在初始安装中???真是天才……):

STATICFILES_FINDERS = ( 
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
   'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

您不需要这些: STATICFILES_DIRS = (os.path.join(BASE_DIR, "static") STATIC_ROOT = '/静态/'

你不需要:

manage.py collectstatic

http://agiliq.com/blog/2013/03/serving-static-files-in-django/#servee:

注意事项

我们没有对 Django 提供给我们的任何静态设置进行任何更改。我们将静态设置保留在 Django 提供的默认 settings.py 中。

您不需要对 urls.py 进行任何更改即可在开发中提供静态文件。您不需要添加 staticfiles_urlpatterns()。很多时候,我对此感到困惑。

您不需要 python manage.py collectstatic 在开发中提供静态文件。

【问题讨论】:

  • 题外话:我个人更喜欢使用{% static 'originaltheme.css' %}(需要{% load static %})而不是使用STATIC_URL var。

标签: django


【解决方案1】:

我也与这个特殊问题作斗争。最后想出了this blog post

其实你不需要collectstatic、STATICFILES_FINDER和django.contrib.staticfiles。它们都是相关的。阅读blog post

【讨论】:

  • 哦,伙计...如果您遵循此建议,请确保您使用的是 2013 年的 django。但在此之前,请从 django 2013 升级并忽略这一切。
  • @DerekAdair,如您所见,这已在 2014 年初得到答复。
【解决方案2】:

我将这些行添加到my url.py 的底部:

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

这个在my settings.py:

STATICFILES_DIRS = ()

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

【讨论】:

    【解决方案3】:

    你用过吗:

    ./manage.py collectstatic
    

    另外,您的 STATIC_ROOT 应该是静态文件应该收集到的目录的绝对路径:

    STATIC_ROOT = os.path.normpath(os.path.join(BASE_DIR, "static"))
    

    【讨论】:

    • 我只是尝试运行它,最终输出是:OSError: [Errno 13] Permission denied: '/static'
    • 现在它告诉我“BASEDIR”没有定义......但它是!正如您在我的代码中看到的那样。 import os BASE_DIR = os.path.dirname(os.path.dirname(file)) TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')] STATICFILES_DIRS = [os.path.join (BASE_DIR, "static")] STATIC_ROOT = os.path.normpath(os.path.join(BASEDIR, "static")) STATIC_URL = '/static/'
    • 你使用 BASE_DIR 并且在我的代码中它是 BASEDIR,修正这个错字,它应该是:STATIC_ROOT = os.path.normpath(os.path.join(BASE_DIR, "static"))
    • 好的。谢谢。我修复了这个问题,现在 ./manage.py collectstatic 给了我这个错误:文件“/Users/NickStefan/Users/NickStefan/Documents/PythonProjects/nickswebsite/.virtualenvs/nickswebsite/lib/python2.7/posixpath.py”,行61、在isabs return s.startswith('/') AttributeError: 'list' object has no attribute 'startswith'
    • 在您的设置中将 [] 更改为 () STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
    【解决方案4】:

    这就是我使用上述答案为 Django 2.0 所做的。因此,您需要一个易于部署的系统。

    settings.py:

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
    # Serve Static in Development
    
    STATICFILES_FINDERS = ( 
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    )
    STATICFILES_DIRS = [os.path.join(BASE_DIR, "appName/static/appName")]
    
    # Serve static in production
    
    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    

    然后创建一个目录 ~/documentRoot/appName/static/appName/ 并将我的 png 复制到其中。然后在我的模板中:

    <head>
      <bootstrap,jquery,etc...>
      {% load staticfiles %}
    </head>
    <body>
    <img class="mb-4" src="{% static "myImage.png" %}" alt="" width="72" height="72">
    </body>
    

    然后,当您想要部署时,您只需运行 collectstatic,它们就会在一个地方提供服务。每次添加应用时,添加到静态目录列表。

    【讨论】:

      猜你喜欢
      • 2012-06-04
      • 2012-01-26
      • 2013-12-22
      • 2012-03-25
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-25
      相关资源
      最近更新 更多