标签: django-staticfiles

如何在Django 1.4中使用django.contrib.staticfiles提供管理静态文件(使用一个Apache服务器)?

Django建议我,如果我只使用一台服务器(Apache)来提供动态和静态文件,那么我应该使用静态文件django.contrib.staticfiles.

所以在我,settings.py我已经加载django.contrib.staticfiles到我INSTALLED_APPSdjango.core.context_processors.static我的TEMPLATE_CONTEXT_PROCESSORS.

我注意到在管理模板中它链接到这样的静态文件(来自index.html):

{% load i18n admin_static %}

{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/dashboard.css" %}" />{% endblock %}
Run Code Online (Sandbox Code Playgroud)

但是看一下模板标签admin_static,它只是一个包装器static:

from django.conf import settings
from django.template import Library

register = Library()

if 'django.contrib.staticfiles' in settings.INSTALLED_APPS:
    from django.contrib.staticfiles.templatetags.staticfiles import static
else:
    from django.templatetags.static import static

static = register.simple_tag(static)
Run Code Online (Sandbox Code Playgroud)

所以我得出结论,因为每个管理静态文件都是带有admin/...前缀的服务器,那么完整路径(对于我的情况)应该是

/usr/lib64/python2.7/site-packages/django/contrib/admin/static
Run Code Online (Sandbox Code Playgroud)

所以我将该路径设置为我的STATICFILES_DIRS内部settings.py,但Apache仍然不会提供任何静态文件(在重新启动服务器之后).我的逻辑在哪里犯了错误?

python apache django django-templates django-staticfiles

6
推荐指数
1
解决办法
6710
查看次数

在Django中将相同的静态文件用于多个应用程序

我的Django项目分为几个应用程序,静态文件存储在每个应用程序中,如下所示:

/Project
   /foo
      /static
         /css
            /style.css
      /views.py
      /models.py
      /urls.py
   /bar
      /static
         /css
            /style.css
      /views.py
      /models.py
      /urls.py
   /urls.py
   /manage.py
   /settings.py
Run Code Online (Sandbox Code Playgroud)

但我不确定如何将相同的静态文件用于多个应用程序.

django django-staticfiles

6
推荐指数
1
解决办法
2497
查看次数

Django/mod_wsgi OSError:[Errno 13]权限被拒绝:当DEBUG = OFF时'静态'

我使用mod_wsgi在Centos 6.2服务器上运行Django 1.4应用程序(运行apache,mysql,php),我的项目部署在虚拟环境中.应用程序本身就是我多年来在托管服务上使用的应用程序本身,我现在部署在我自己的rackspace云服务器上.该应用程序的确切版本在其他地方运行良好,所以这个问题是关于我如何部署它.这是我的第一个python/django部署 - 之前我已经建立了几十个LAMP站点,所以我对Django部署的熟悉程度显然让我无法接受.

该应用程序在我的服务器上使用DEBUG = TRUE项目的settings.py文件正常工作,但是当我将其更改为FALSE时,站点的前端会产生[500]内部服务器错误.

据我所知,有DEBUG设置为OFF,Apache是目前通过mod_wsgi的服务我的静态文件(mod_wsgi的工作正常),这使我相信我的配置"东西"是防止这一点.我已经运行了./manage.py collectstatic填充文件夹中/static目录的命令/myproject.

我已经在这方面工作了几个星期,阅读了尽可能多的部署指南,但到目前为止还没有任何乐趣.非常感谢您的协助.

以下是我项目的settings.py文件中的相关声明:

############ settings.py #############

SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
MEDIA_ROOT = '/opt/virtual/myproject/static/localtv/media/'
MEDIA_URL = 'http://example.org/static/localtv/media/'
STATIC_ROOT = '/opt/virtual/myproject/static/'
STATIC_URL = 'http://example.org/static/'
Run Code Online (Sandbox Code Playgroud)

这是wsgi.py文件:

############# wsgi.py #################
import os
import sys
import site
site.addsitedir('/opt/virtual/myapp/lib/python2.7/site-packages')

apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)

sys.path.append('/opt/virtual')
sys.path.append('/opt/virtual/myproject')

os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Run Code Online (Sandbox Code Playgroud)

我在httpd.conf中的虚拟主机声明如下所示:

############ virtual host declaration in httpd.conf ##############
<VirtualHost *:80>
ServerName example.org
ServerAlias www.example.org
DocumentRoot /opt/virtual/myproject …
Run Code Online (Sandbox Code Playgroud)

python django permissions mod-wsgi django-staticfiles

6
推荐指数
1
解决办法
7058
查看次数

django collectstatic overriding

我正在使用Django 1.3.1和contrib.collectstatic应用程序来管理我的静态文件.

我的项目结构是

myproject
    - settings.py
    - static-media
    - urls.py
    - media
    - manage.py
Run Code Online (Sandbox Code Playgroud)

其中static-media是包含此项目的静态文件的文件夹.在我的settings.py中,我有:

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, "static")+'/'
STATIC_URL = "/static/"
STATICFILES_DIRS = (
        os.path.join(PROJECT_PATH, 'static-media'),
)
Run Code Online (Sandbox Code Playgroud)

我正在使用admin_tools来更改管理员的布局.但是我想从admin_tools覆盖特定的css文件(theming.css).所以在我的静态媒体文件夹中我放了admin_tools/css/theming.css.当我第一次运行时python manage.py collectstatic,它通过忽略admin_tools中的默认的theming.css并使用我在static-media中定义的那个来按预期工作.不幸的是,如果我再次运行该命令,它会覆盖我的css并添加默认值.

这是输出python manage.py findstatic admin_tools/css/theming.css:

Found 'admin_tools/css/theming.css' here:
  /home/paulo/Desktop/Projects/zennetwork/prd/zennetwork/static-media/admin_tools/css/theming.css
  /home/paulo/Desktop/Projects/zennetwork/prd/lib/python2.7/site-packages/admin_tools/theming/static/admin_tools/css/theming.css
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.谢谢.

python django django-staticfiles

6
推荐指数
1
解决办法
6409
查看次数

Django | 连接路径位于基本路径组件之外{%static img.thumbnail.url%},错误400与whitenoise

我已经完成了我在Django的第一个应用程序并且工作得很好,但是因为我设置了DEGUG = False,所以仍然有预部署问题...这里只是在模板中显示图像... T_T

我正在使用它,但现在当我使用whitenoise为我的图像提供服务时它无法工作......并且它返回错误请求(400)错误...

Models.py

class GalleryItem(models.Model):
    thumbnail = models.ImageField(blank=True,upload_to='gallery/thumb')
    img_wide = models.ImageField(blank=True,upload_to='gallery')
Run Code Online (Sandbox Code Playgroud)

template.py

{% load staticfiles %}
{% for img in img_to_display %}
    <a href="{{ img.img_wide.url}}" class="swipebox" title="">
        <img src="{% static img.thumbnail.url %}" alt="{{ img.alt}}">
    </a>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

urls.py

from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
import os

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^gallery/', include('gallery.urls')),
    url(r'^shop/', include('shop.urls')),
    url(r'^events/', include('events.urls')),
    url(r'^page/', include('paginator.urls')),
    url(r'^news/', include('blog.urls')),
    url(r'^ckeditor/', include('ckeditor_uploader.urls')), …
Run Code Online (Sandbox Code Playgroud)

python django bad-request django-staticfiles

6
推荐指数
2
解决办法
1万
查看次数

djangocollectstatic 与 django-storages 重新复制所有文件

我使用 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 为 …

django amazon-s3 django-storage django-staticfiles

5
推荐指数
1
解决办法
1563
查看次数

姜戈。列出静态文件夹中的文件

我遇到的一个看似基本的事情是将一个简单的静态文件列表(比如我的服务器上单个存储库目录的内容)呈现为链接列表。这是否安全是另一个问题,但假设我想这样做......这就是我的工作目录的样子。我想在我的模板中列出来自分析文件夹的所有文件,作为链接。

在此处输入图片说明
我已经尝试按照一些教程访问 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)

python django django-staticfiles

5
推荐指数
1
解决办法
4777
查看次数

Django 加载 ES6 javascript 文件

我想在 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

5
推荐指数
1
解决办法
3955
查看次数

当我运行react脚本“yarn start”时,我到index.html中的manifest.json的链接有效,但当我运行“python3 manage.py runserver”时则无效

当我运行“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)

html json python-3.x django-staticfiles reactjs

5
推荐指数
1
解决办法
4654
查看次数

在 django 中从私有 s3 提供静态文件

有没有办法从私有 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 amazon-s3 django-staticfiles

5
推荐指数
1
解决办法
4062
查看次数