我知道这个问题已经在这里被问过好几次了。我搜索并阅读了许多答案,但没有帮助。我想,我错过了一些非常基本的东西。
在我的 settings.py 中,我有:
STATIC_URL = '/static/'
STATIC_ROOT = join(APPS_DIR, "static/")
# STATICFILES_DIRS = [join(APPS_DIR, 'static')]
MEDIA_ROOT = join(APPS_DIR, 'media')
MEDIA_URL = "/media/"
Run Code Online (Sandbox Code Playgroud)
在我的 config/urls.py 中,我有:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Run Code Online (Sandbox Code Playgroud)
我有一个文件,/static/core/js/jquery_countdown/jquery.countdown.min.js我试图将其加载到模板中,如下所示:
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="{% static 'core/js/jquery_countdown/jquery.countdown.min.js' %}"> </script>
Run Code Online (Sandbox Code Playgroud)
同一模板的顶部看起来像
{% extends "contest/base.html" %}
{% load static %}
Run Code Online (Sandbox Code Playgroud)
这会导致以下服务器错误:
[23/Mar/2018 10:12:08] "GET /static/core/js/jquery_countdown/jquery.countdown.min.js HTTP/1.1" 404 1858
Run Code Online (Sandbox Code Playgroud)
我缺少什么?
我有以下代码块,其中.css返回相应的文件路径。
它是Theme-Class允许用户通过配置文件视图中的按钮更改网站主题(深色和浅色)的一部分。
def link(self) -> str:
"""
Returns the link where the CSS file for this theme is located
"""
return static('app_shared/colors_%s.css' % self.name())
Run Code Online (Sandbox Code Playgroud)
当它发生在 HTML-Template 中时,同样的问题可以通过更改{% load staticfiles %}为{% load static %}. 显然,对于源代码,我需要另一种选择。
当我构建一个应用程序时显示此错误并且无法修复它时,我不知道如何处理这个问题,你能帮助我吗?
Internal Server Error: /
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/usr/local/lib/python3.10/dist-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/abolfazl/socialmedia/core/views.py", line 6, in index
return render(request, 'index.html')
File "/usr/local/lib/python3.10/dist-packages/django/shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/usr/local/lib/python3.10/dist-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/usr/local/lib/python3.10/dist-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/usr/local/lib/python3.10/dist-packages/django/template/base.py", line 170, in render
return self._render(context)
File "/usr/local/lib/python3.10/dist-packages/django/template/base.py", line 162, …Run Code Online (Sandbox Code Playgroud) 我正在使用django-compressor和django-storage来在S3上提供我的压缩文件(使用这些说明:http://django_compressor.readthedocs.org/en/latest/remote-storages/#using-staticfiles).它在运行"compress"管理命令后最初工作得很好,但大约一小时后,压缩的css和js文件返回403 Forbidden错误,即使我没有对文件进行任何更改.我似乎无法隔离问题,所以任何帮助将不胜感激.
以下是我使用的设置:
COMPRESS_ENABLED = True
COMPRESS_URL = "http://mybucket.s3.amazonaws.com/"
COMPRESS_STORAGE = 'sm.storage.CachedS3BotoStorage'
COMPRESS_YUI_BINARY = os.path.join(PROJECT_ROOT, 'jars/yuicompressor-2.4.7.jar')
COMPRESS_CSS_FILTERS = ['compressor.filters.yui.YUICSSFilter',
'compressor.filters.css_default.CssAbsoluteFilter']
COMPRESS_JS_FILTERS = ['compressor.filters.yui.YUIJSFilter',]
COMPRESS_OFFLINE = True
STATICFILES_STORAGE = COMPRESS_STORAGE
STATIC_URL = COMPRESS_URL
STATIC_ROOT = '/path/to/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage' # I'm using this for uploaded media
AWS_ACCESS_KEY_ID = 'myaccesskey'
AWS_SECRET_ACCESS_KEY = 'mysecretkey'
AWS_STORAGE_BUCKET_NAME = 'mybucket'
AWS_S3_FILE_OVERWRITE = True
AWS_HEADERS = {
'Cache-Control': 'public, max-age=31536000', #(1 year)
}
Run Code Online (Sandbox Code Playgroud)
更新:当COMPRESS_OFFLINE为True时,这似乎只是一个问题.我将其设置为False,并且在初始请求期间创建的压缩文件正常工作,并且已经超过一个小时.但是,我更喜欢使用管理命令预压缩这些文件.
我来自PHP背景,想要STATICFILES_DIRS
在settings.py和windows中提供路径
我明白我需要提供完整的路径:
D:/path/to/folder/project/static
我想知道有没有办法给出一些像"/ static /"这样的路径我们可以像dirname(__FILE__)PHP 一样给出路径吗?
所以我的路径没有硬编码.另外为什么它是物理路径,因为它使用http路径通常不遵循加载CSS文件的物理路径?那么为什么这种方式(物理路径)在django?有人可以解释一下.
感谢大家的提前努力.
我正在使用 - Django 1.2 - Apache - Mod WSGI开发一个门户网站
我有几个由apache提供的HTML文件.
我想在我的基本模板下呈现这些静态HTML页面,以保持页眉/页脚和动态菜单的完整性.
我可以想到使用iframe的一种方式.另一种方式是做阅读HTML文件,并返回的字符串,而渲染,但在这种情况下,我失去优势的Apache,所以我想知道是否会有这样做的没有更好的办法,就是通过有Django的东西提供任何现有的解决方案?
我在forms.py中有以下表格
from django import forms
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(label='Password', widget=forms.PasswordInput)
Run Code Online (Sandbox Code Playgroud)
在我的templates/login.html文件中,我有以下内容:
{% load staticfiles %}
<div>
<form action="" method="post" style="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="SignIn">
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
我想为ui使用bootstrap工具,[ http://getbootstrap.com/examples/signin/]我将在login.html中加载一些js和css静态文件
当我使用模板标签获取表单时,如何将css效果应用于我的登录表单{{ form.as_p }}?
我在Debian 8 VPS上部署Django应用程序时遇到了麻烦.Python版本2.7,Django 1.10.2.
我的问题是,即使在运行'collectstatic'并分配STATIC_ROOT目录之后,它也不会在生产模式下提供静态文件(DEBUG = False).
我已经遵循了有关此部署的每条指令(nginx,uwsgi,python),但仍然可以获得所有静态文件的404.运行collectstatic时,它会将所有文件放入应用程序顶部的/ static /目录中.当我运行uwsgi或开发服务器时,HTML和python函数很好,但我的所有静态CSS,JS,IMG都无法访问.
当我切换回DEBUG = True并运行开发服务器时,我的静态文件再次出现.
有人可以看看,看看我做错了什么?如果您需要更多文件上下文,请告诉我们.
我的settings.py内容如下:
"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 1.10.2.
For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret …Run Code Online (Sandbox Code Playgroud) 我知道这个问题已被问过很多次,但我仍然无法让它发挥作用.我的Env配置:
在我的requrements.txt中,我有以下依赖项
我storages在INSTALLED_APPS中添加了.这些是我的设置参数.
AWS_ACCESS_KEY_ID='****'
AWS_SECRET_ACCESS_KEY='****'
AWS_STORAGE_BUCKET_NAME='****'
AWS_AUTO_CREATE_BUCKET = False
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = DEFAULT_FILE_STORAGE
AWS_LOCATION = 'static/'
Run Code Online (Sandbox Code Playgroud)
当我运行该./manage.py collectstatic命令时,我收到以下错误:
File "/home/me/myproj/local/lib/python2.7/site-packages/storages/backends/s3boto.py", line 23, in <module>
raise ImproperlyConfigured("Could not load Boto's S3 bindings.\n"
django.core.exceptions.ImproperlyConfigured: Could not load Boto's S3 bindings.
See https://github.com/boto/boto
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?我没有正确配置什么?
是我在Django中的第一个应用程序,我正在尝试准备将Django(2.0)应用程序用于生产,但是我无法使用WhiteNoise使静态文件正确加载
我一直一直在日志中遇到下一个错误
ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
ValueError: Missing staticfiles manifest entry for 'css/inicio.css'
[02/Jun/2018 14:40:37] ERROR [django.server:124] "GET /participation/prueba HTTP/1.1" 500 27
Run Code Online (Sandbox Code Playgroud)
我有以下settings.py
...
DEBUG=False
DJANGO_APPS = ['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
#Delete for development whitenoise.runserver_nostatic
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
'django.contrib.sites'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
STATICFILES_DIRS = (
(os.path.join(BASE_DIR, 'static')),
)
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
Run Code Online (Sandbox Code Playgroud)
我在根目录下将所有静态文件都放在一个名为static的文件夹中,当我运行manage.py collectstatic时,我会在staticfiles目录中生成所有静态文件,但是无论如何我还是无法使其运行。
我尝试找出问题所在,并且正在使用以下模板
<!DOCTYPE …Run Code Online (Sandbox Code Playgroud) python django production-environment django-staticfiles whitenoise
django ×10
python ×6
amazon-s3 ×2
apache ×1
boto ×1
boto3 ×1
css ×1
django-3.0 ×1
html ×1
javascript ×1
static-files ×1
whitenoise ×1