我想在弹性豆茎上放一个简单的django应用程序.我认为我已经找到了应用程序的静态部分,因为它与heroku和手动设置的服务器一起使用.在调试中我甚至检查了静态目录中的静态文件以尝试简化操作.映射似乎很奇怪,因为它似乎不遵循STATIC_ROOT.
我的相关配置:settings.py
PROJECT_ROOT = os.path.abspath(os.path.dirname(__name__))
STATIC_ROOT = os.path.join(PROJECT_ROOT,'static/')
STATIC_URL = '/static/'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
Run Code Online (Sandbox Code Playgroud)
urls.py
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
Run Code Online (Sandbox Code Playgroud)
日志:
[Wed Dec 26 15:39:04 2012] [error] [client 10.29.203.20] File does not exist: /opt/python/current/app/css, referer 10.29.203.20 - -
[26/Dec/2012:15:39:04 +0000] "GET /static/css/styles.css HTTP/1.1" 404 329 "http://" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.101 Safari/537.11"
Run Code Online (Sandbox Code Playgroud) django amazon-web-services django-staticfiles amazon-elastic-beanstalk
我一直在使用s3boto的S3BotoStorage作为我的静态文件后端,并使用./manage.py collectstatic将文件同步到我的aws s3存储桶(暂存和生产).它工作正常.但是它很慢.除了我自己的静态文件(只有几个)和django admin之外,我还有一些包含许多静态文件的第三方软件包(grappelli,django-redactor).每次运行它时,collectstatic可能需要15分钟,具体取决于我的互联网连接.对于我正在与我的临时存储桶同步并且事情不太正确的情况,我必须调整一些内容并重新同步,这是一个重要的时间杀手.是否有任何好的,快速的,可编写脚本的替代方法,用于将静态文件同步到s3?
我不确定它们的区别是什么,看起来它们都在起作用.我用Google搜索,似乎它们几乎是一样的.只是出于好奇,人们在现场使用哪一个?
我读过但仍然不知道何时使用哪个,以及该领域的哪个人使用.我的工作都适合他们.起初我以为它是加载静态文件夹,但它也适用于静态文件... -
好像有几次问这个问题,但我无法解决.
我在生产中部署了一个django应用程序DEBUG = False.我设定了我的allowed_host.我{% load static from staticfiles %}以前加载静态文件.我正好写了Heroku doc提取的设置:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
Run Code Online (Sandbox Code Playgroud)
但是我得到了一个错误500.并得到了这个追溯(通过邮件)
...
`cache_name = self.clean_name(self.hashed_name(name))
File "/app/.heroku/python/lib/python3.5/site- packages/django/contrib/staticfiles/storage.py", line 94, in hashed_name (clean_name, self))
...
ValueError: The file ‘app/css/font.css’ could not be found with <whitenoise.django.GzipManifestStaticFilesStorage object at 0x7febf600a7f0>.`
Run Code Online (Sandbox Code Playgroud)
当我运行heroku run python manage.py collectstatic --noinput
All似乎没问题:
276 static files copied to '/app/annuaire/staticfiles', …
我正在使用django staticfiles + django- storages 和Amazon S3来托管我的数据.一切正常,但每次运行manage.py collectstatic命令时都会将所有文件上传到服务器.
看起来管理命令比较Storage.modified_time()了django-storages在S3存储中未实现的时间戳.
你们如何确定S3文件是否被修改过?
我可以在我的数据库中存储文件路径和最后修改的数据.或者有一种简单的方法从亚马逊拉出最后修改过的数据?
另一个选择:看起来我可以分配任意元数据python-boto,我可以在第一次上传时放置本地修改日期.
无论如何,这似乎是一个常见问题,所以我想问一下其他人使用过什么解决方案.谢谢!
我有一个渲染图像的模板:
{% load staticfiles %}
<img src="{% static "img/logo.png" %}" alt="My image"/>
Run Code Online (Sandbox Code Playgroud)
图像链接已断开,但它指向:
localhost/static/img/logo.png
Run Code Online (Sandbox Code Playgroud)
我需要为static_root,static_url和STATICFILES_DIRS设置哪些值才能使此图像正确显示?
这是我的目录结构:
myprojectname(顶级)
--- myprojectname
--- --- myproectname
--- --- ---设置
--- --- --- --- base.py(setting.py)
- - - - 静态的
--- --- --- img
这是我在设置中的静态配置:
STATIC_ROOT = '/Users/myuser/myprojectname/myprojectname'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
#normpath(join(SITE_ROOT, 'static')),
os.path.join(BASE_DIR, "static"),
'/Users/myuser/myprojectname/myprojectname/static',
)
Run Code Online (Sandbox Code Playgroud)
这就是它所显示的:

我已经完成了一个收集站,这不起作用.
我一直在打破这一整天,但无法弄清楚问题.它是在我将项目从一台机器复制到另一台机器后发生的.
Settings.py
STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
Run Code Online (Sandbox Code Playgroud)
在INSTALLED_APPS中也提到了'django.contrib.staticfiles'.
文件夹结构:
Django-Projects (root)
project
app
static
css
home.css
js
manage.py
Run Code Online (Sandbox Code Playgroud)
模板:
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/home.css' %}">
Run Code Online (Sandbox Code Playgroud)
urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'', include('app.urls')),
)
Run Code Online (Sandbox Code Playgroud)
它在打开模板时在控制台中引发错误:
GET http://127.0.0.1:8000/static/css/home.css
Failed to load resource: the server responded with a status of 404 (NOT FOUND)
Run Code Online (Sandbox Code Playgroud)
这可能有什么问题?请帮帮我.非常感谢!
我一整天都在思考这个问题,但无法弄清楚问题所在。它发生在我将我的 Django 项目从一台 PC 复制到另一台之后。
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Users/username/opt/anaconda3/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/Users/username/opt/anaconda3/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS) …Run Code Online (Sandbox Code Playgroud) 我试图让staticfilestaglib在我的应用程序中运行时遇到一个非常奇怪的问题.我基本上得到以下错误:
'staticfiles' is not a valid tag library: Template library staticfiles not found, tried django.templatetags.staticfiles,django.contrib.admin.templatetags.staticfiles
Run Code Online (Sandbox Code Playgroud)
这是我的模板,它抛出了这个错误:
{% load staticfiles %}
<html>
<head>
{% block stylesheets %}
<link rel="stylesheet" href="{% static "styles/bootstrap-1.2.0.min.css" %}">
{% endblock %}
<title>{% block title %}Tzibor{% endblock %}</title>
</head>
<body>
<h1>It Works!</h1>
{% block scripts %}
<script type="text/javascript" src="{% static "scripts/jquery-1.6.2.min.js" %}"></script>
{% endblock %}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我的settings.py:
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
)
MANAGERS = ADMINS
DATABASES = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用django-pipeline来缩小静态资源,为它们使用缓存并使我的模板更简单.我的浏览器找到并加载了我的CSS和JS文件,但我的(非常简单的)主页加载大约需要10秒钟.

我使用的是Python 2.7.6,Django 1.7.3和django-pipeline 1.4.3.PyCharm使用本地virtualenv运行开发服务器.
我的settings.py包含以下内容:
DEBUG = True
TEMPLATE_DEBUG = DEBUG
INSTALLED_APPS = (
'django_admin_bootstrapped', # custom admin
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# pip installed apps
'pipeline',
# project apps
'myapp',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'pipeline.middleware.MinifyHTMLMiddleware',
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'pipeline.finders.FileSystemFinder',
'pipeline.finders.CachedFileFinder',
'pipeline.finders.PipelineFinder',
)
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'myapp/static'),
)
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
PIPELINE_CSS_COMPRESSOR = 'pipeline.compressors.yuglify.YuglifyCompressor'
PIPELINE_JS_COMPRESSOR = …Run Code Online (Sandbox Code Playgroud)