我正在尝试使用django-cumulus来提供Rackspace CloudFiles文件.我目前只在我的本地开发服务器上使用Django 1.4.2进行尝试.
我可以使用cumulus的syncstatic管理命令成功上传我的所有静态资产,但我似乎无法使用相同的设置在我的网站上显示它们.
如果我的相关设置是:
STATIC_URL = '/static/'
CUMULUS = {
'USERNAME': 'myusername',
'API_KEY': 'myapikey',
'CONTAINER': 'mycontainername',
'STATIC_CONTAINER': 'mycontainername',
}
DEFAULT_FILE_STORAGE = 'cumulus.storage.CloudFilesStorage'
STATICFILES_STORAGE = 'cumulus.storage.CloudFilesStaticStorage'
Run Code Online (Sandbox Code Playgroud)
然后,当我运行syncstatic我的所有应用程序的静态文件上传到/mycontainername/static/,正如我所期望的那样.但是当我在管理员中加载页面时,它会忽略STATIC_URL并尝试从URL http://uniquekey....r82.cf2.rackcdn.com/path/to/file.css而不是URL中提供资源http://uniquekey....r82.cf2.rackcdn.com/static/path/to/file.css.
此外,我无法看到如何让我的公共(非管理员)页面使用CloudFiles上的静态文件,而不是从本地/static/目录提供它们.
我错过了一些关键的设置,还是我做错了什么?
我正在尝试使用 mod_python 将代码上传到 apache 服务器上。我已经尝试了很多,但服务器无法访问我的静态文件(我所有的图像、js 和 css)。这是我的虚拟主机设置:
<VirtualHost *:80>
ServerName mysite.com
ServerAlias www.mysite.com
Alias /static/ /home/mysite/products/static/
#
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mysite\.com
RewriteRule (.*) http://mysite.com$1 [R=301,L]
#
DocumentRoot /home
<Directory /home/mysite/>
SetHandler mod_python
PythonHandler mod_python.publisher
PythonDebug On
</Directory>
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog …Run Code Online (Sandbox Code Playgroud) 好的,我的
Index.html是
<!DOCTYPE html>
<html>
<head>
<title>Kodeworms</title>
<link rel="stylesheet" href="{{ STATIC_URL }}css/style.css" />
</head>
<body class="logged-out">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
style.css文件
.logged-out {
background-image: href=("{{ STATIC_URL }}img/landing.jpg") no-repeat center 30px;
background-size: 90%;
}
Run Code Online (Sandbox Code Playgroud)
现在我的index.html存储在*project_name/project_name/templates*中 ,我的style.css存储在*project_name/assets/css*中 ,图像存储在*project_name/assets/img*中
我的setting.py是
# Django settings for BE.
import os
import dj_database_url
here = lambda * x: os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
PROJECT_ROOT = here("..")
root = lambda * x: os.path.join(os.path.abspath(PROJECT_ROOT), *x)
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email@example.com'),
)
MANAGERS = …Run Code Online (Sandbox Code Playgroud) 我使用 django.contrib.staticfiles 和 django-storages 将静态文件部署到 Amazon S3。我使用的 django 版本是 1.10.4,django-storages 版本是 1.5.2。
现在,当我运行collectstatic时,即使本地文件没有更改,它也会将所有文件从本地系统重新复制到S3。查看collectstatic管理命令代码我可以看到:
在方法delete_file中:
# The full path of the target file
if self.local:
full_path = self.storage.path(prefixed_path)
else:
full_path = None
# Skip the file if the source file is younger
# Avoid sub-second precision (see #14665, #19540)
if (target_last_modified.replace(microsecond=0) >= source_last_modified.replace(microsecond=0) and
full_path and not (self.symlink ^ os.path.islink(full_path))):
if prefixed_path not in self.unmodified_files:
self.unmodified_files.append(prefixed_path)
self.log("Skipping '%s' (not modified)" % path)
return False
Run Code Online (Sandbox Code Playgroud)
在调试时,我看到即使 target_last_modified >= source_last_modified 但 full_path 为 …
我遇到的一个看似基本的事情是将一个简单的静态文件列表(比如我的服务器上单个存储库目录的内容)呈现为链接列表。这是否安全是另一个问题,但假设我想这样做......这就是我的工作目录的样子。我想在我的模板中列出来自分析文件夹的所有文件,作为链接。
我已经尝试按照一些教程访问 view.py 中的静态文件并像这样:
view.py
from os import listdir
from os.path import isfile, join
from django.contrib.staticfiles.templatetags.staticfiles import static
def AnalyticsView(request):
mypath = static('/analytics')
allfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
return render_to_response('Rachel/analytics.html', allfiles)
Run Code Online (Sandbox Code Playgroud)
还有我的模板:
<p>ALL FILES:</p>
{% for file in allfiles %}
{{ file }}
<br>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
还有我的 settings.py
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
Run Code Online (Sandbox Code Playgroud)
我收到错误:
FileNotFoundError at /analytics/
[WinError 3] The system …Run Code Online (Sandbox Code Playgroud) 我的 Django 站点用户端使用静态文件运行良好,但不知道为什么所有管理面板静态文件都不起作用。虽然它可以正常工作,但对 linux 没有任何想法??
nginx.conf 文件
upstream sample_project_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/home/me/SPEnv/run/gunicorn.sock fail_timeout=0;
}
server {
listen 800;
server_name <your domain name>;
client_max_body_size 4G;
access_log /home/me/logs/nginx-access.log;
error_log /home/me/logs/nginx-error.log;
location /static {
root /home/me/DjangoProjects/SP/SP;
}
location / {
# an HTTP header important enough to have its own Wikipedia …Run Code Online (Sandbox Code Playgroud) 我在本地机器上使用 django。为了提供静态文件,我将 WhiteNoise 与它一起使用。当DEBUG = True所有静态文件都正确提供时。但是当我更改DEBUG = False并设置时,ALLOWED_HOSTS = ['*']我收到了 500 服务器错误。但是管理站点加载没有任何错误。此外,当我注释掉时,STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'我没有收到 500 错误。
我按照http://whitenoise.evans.io/en/stable/django.html 中给出的文档来连接 whitenoise。我没有对wsgi.py文件进行任何更改。我跑了python manage.py collecststatic,它没有任何错误地运行。
下面settings.py给出:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'fdft&b(xb*!qq3ghjkjhg6789ih8ik!w10$0uscxcpqpmz'
DEBUG = False
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'whitenoise.runserver_nostatic', #Disable Djangos static file server during DEVELOPMENT
'gep_app.apps.GepAppConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware', …Run Code Online (Sandbox Code Playgroud) django django-settings django-staticfiles collectstatic whitenoise
我想在 Django 的 index.html 中使用 ES6 中的导入功能。
我不想编译到 ES5 以实现浏览器兼容性。我想假设所有用户都拥有兼容 ES6 的浏览器。
因此我不需要像 Babel 这样的 ES6 到 ES5 编译器:https : //github.com/kottenator/django-compressor-toolkit
如果可以的话,我只想提供 ES6 Javascript 和浏览器来编译它。
我试过:
<!-- Index.html -->
<script type="module" src="{% static 'app.js' %}"></script>
Run Code Online (Sandbox Code Playgroud)
//app.js
(function(){
console.log("Hello from App.js");
})();
Run Code Online (Sandbox Code Playgroud)
# settings.py
if DEBUG:
import mimetypes
mimetypes.add_type("module", ".js", True)
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
加载模块脚本失败:服务器以“text/plain”的非 JavaScript MIME 类型响应。每个 HTML 规范对模块脚本执行严格的 MIME 类型检查。
更新 1:我试过:
<script type="module" src="{% static 'app.js' %}"></script>
<script nomodule src="{% static 'app.js' %}"></script>
<script type="module">import "{% static 'main.mjs' …Run Code Online (Sandbox Code Playgroud) django django-templates django-staticfiles ecmascript-6 es6-modules
当我运行“yarn start”时,我的index.html 文件中的manifest.json 链接工作正常,但是当我运行时,'python3 manage.py runserver'我在终端中得到的所有内容是:
Not Found: /manifest.json
"GET /manifest.json HTTP/1.1" 404 2234
Run Code Online (Sandbox Code Playgroud)
我的所有静态链接和导入也会发生这种情况。我对 Django 和 React 以及整个编程都很陌生,所以我认为我只是错过了一些简单的东西,但我无法弄清楚。
我一直在尝试使用,但即使我进行编辑以指向我的目录{% load static %},该链接也不起作用。我还尝试编辑and ,但我得到的只是终端中的语法错误。除此之外我一无所知。STATIC_URLsettings.pymanifest.jsonview.pyurls.py
前端/public/index.html
Not Found: /manifest.json
"GET /manifest.json HTTP/1.1" 404 2234
Run Code Online (Sandbox Code Playgroud)
前端/urls.py
<html>
<head>
<title>WebProject</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
<link rel="manifest" href="manifest.json"/>
</head>
<body style="background-color: #FAF0E6; font-family: Verdana; font-size: 40px;">
<div id="root"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
前端/views.py
from django.urls import path
from . import views
from django.conf.urls.static import static …Run Code Online (Sandbox Code Playgroud) 有没有办法从私有 s3.conf 提供静态文件?我知道有很多教程(例如https://simpleisbetterthancomplex.com/tutorial/2017/08/01/how-to-setup-amazon-s3-in-a-django-project.html)可以帮助您提供服务来自公共存储桶的静态文件。
我提供我的媒体文件。我得到一个签名的 url,它将在 120 秒后超时。我无法获得静态文件的类似 url 吗?
如果这听起来很愚蠢,我很抱歉。
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'assets'),]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
DEFAULT_FILE_STORAGE ='storages.backends.s3boto3.S3Boto3Storage'
AWS_ACCESS_KEY_ID = "***************"
AWS_SECRET_ACCESS_KEY ="**********"
AWS_STORAGE_BUCKET_NAME = "*********"
AWS_S3_REGION_NAME = 'us-east-2'
AWS_S3_SIGNATURE_VERSION = 's3v4'
AWS_DEFAULT_ACL = None
AWS_QUERYSTRING_EXPIRE = 120
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_LOCATION = 'static'
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
Run Code Online (Sandbox Code Playgroud)
这是我的settings.py 和
我的媒体文件...当我查询时,我得到一个签名的网址...但是我的静态文件...我没有得到签名的网址(即它看起来像https://bucketname.s3.us-east-2 .amazonaws.com/Path/file_fileext/)
所以我得到了 403 禁止
笔记:
我的存储桶 iam 用户具有 AmazonS3FullAccess 权限
我的存储桶策略是空的
我的阻止所有公共访问均已开启
和
这是我的 cors …
django ×9
amazon-s3 ×2
html ×2
python ×2
apache ×1
css ×1
ecmascript-6 ×1
es6-modules ×1
gunicorn ×1
json ×1
mod-python ×1
nginx ×1
python-3.x ×1
reactjs ×1
whitenoise ×1