这三个静态URL有什么区别?
我不确定我是对的,我MEDIA_ROOT用来存储我上传的照片(通过models.ImageField())
但是,我为我的管理员创建了一个JS脚本admin.py.我将媒体定义如下:
....
class Media:
js = ('/admin/custom.js', )
Run Code Online (Sandbox Code Playgroud)
和我的settings.py:
....
STATIC_ROOT = "/home/user/project/django1/top/listing/static"
Run Code Online (Sandbox Code Playgroud)
我添加了custom.js到STATIC_ROOT/admin/custom.js,但它不工作.投掷404未找到错误.
然后我改变STATIC_ROOT对STATICFILES_DIRS,和它的作品!
....
STATICFILES_DIRS = "/home/user/project/django1/top/listing/static"
Run Code Online (Sandbox Code Playgroud)
所以,我不明白这里发生了什么.事实上,我只是不明白STATIC_ROOT和之间有什么区别STATICFILES_DIRS.
目前我正在通过virtualenv在我的机器上测试Django,还没有部署,这是STATIC_ROOT不工作的原因??
我的CSS文件中有一个引用静态图像的引用:
#logo
{
background: url('/static/logo.png')
}
Run Code Online (Sandbox Code Playgroud)
这在我的开发机器上运行得很好,但在我的生产环境中没有,因为url应该是static.mydomain.com/logo.png.如何根据设置文件中的STATIC_URL动态更改css文件?
服务器返回
TemplateSyntaxError at /
Invalid block tag: 'static'
此行:<img src="{% static 'icon/logo.png' %}">.
whold html文件是这样的(它是另一个html文件{%include%}):
{% load staticfiles %}
<div class="header">
<nav>
<ul class="nav nav-pills pull-right">
<li role="presentation"><a href="{% url 'offer rank' %}">???</a></li>
<li role="presentation"><a href="{% url 'user rank' %}">???</a></li>
<li role="presentation"><a href="#" data-toggle="modal" data-target="#start">??</a></li>
<li role="presentation"><a href="#" data-toggle="modal" data-target="#start">??</a></li>
{% if debug_users %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">????<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
{% for debug_user in debug_users %}
<li><a href="{% url 'debug login' debug_user.id …Run Code Online (Sandbox Code Playgroud) 我正在创建一个Django项目.我刚刚尝试将该项目从调试中删除,DEBUG = False并且出于某种原因,我的所有静态文件都没有显示出来.它们的错误代码为500.我该如何解决这个问题?
一些settings.py:
DEBUG = True
TEMPLATE_DEBUG = DEBUG
...
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
Run Code Online (Sandbox Code Playgroud) 我是一个Django新手在我的第一个项目工作,并有静态文件的问题.
我使用django.contrib.auth两个模板创建了一个简单的auth系统:mysite/templates/index.html和mysite/templates/registration/login.html.我有全局静态内容mysite/static,我希望能够访问所有应用程序呈现的所有模板.
mysite/templates/index.html包含在我访问网址时<img src="{{ STATIC_URL }}pics03.jpg"/>呈现为"static/pics03.jpg"和加载的内容localhost:8000/
mysite/templates/registration/login.html包含当我访问网址时<img src="{{ STATIC_URL }}pics03.jpg"/>也会呈现为"static/pics03.jpg"和不加载的包含"localhost:8000/accounts/login/"
在我的urls.py中,我有:
urlpatterns = patterns('',
url(r'^$', 'mysite.views.home'), # plays index.html template
url(r'^accounts/login/$', 'django.contrib.auth.views.login'),
Run Code Online (Sandbox Code Playgroud)
在我的settings.py中,我有:
PROJECT_DIR = os.path.dirname(__file__)
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(PROJECT_DIR,'static'),
)
STATICFILES_FINDERS = ( …Run Code Online (Sandbox Code Playgroud) 我不确定它们的区别是什么,看起来它们都在起作用.我用Google搜索,似乎它们几乎是一样的.只是出于好奇,人们在现场使用哪一个?
我读过但仍然不知道何时使用哪个,以及该领域的哪个人使用.我的工作都适合他们.起初我以为它是加载静态文件夹,但它也适用于静态文件... -
我正在使用内置静态应用程序的Django 1.3.
我的静态文件夹结构是这样的:
static/
css/
main.css
img/
js/
Run Code Online (Sandbox Code Playgroud)
所以我尝试static/css/img/从CSS中引用文件夹下的图像,如下所示:
background:url('img/btn_white.gif') repeat-x;
Run Code Online (Sandbox Code Playgroud)
但图像不会出现.当我检查Chrome中的元素时,我找到了图像路径http://localhost/mysite/static/css/main.css/img/btn_white.gif/
这是非常奇怪的,因为这个相对路径应该有引用static/css/文件夹而不是main.css.所以我尝试改变路径url('../img/btn_white.gif'),它适用于Chrome和Firefox,但不适用于IE.
我很确定这个问题与Django有关,因为在我的纯HTML/CSS中,这个相对路径运行得很好.我也尝试将css放在媒体文件夹中,问题是一样的.
我的设置与静态应用相关:
在settings.py中:
STATIC_ROOT = os.path.join(os.path.dirname(__file__),'static').replace('\\','/')
STATIC_URL = 'http://localhost/mysite/static/'
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)
我希望在我的 django 项目中使用 date-fns,但不完全确定如何继续 - 我不能依赖 CDN,需要以某种方式安装它。我已经npm init在我的根文件夹中运行,然后是npm install date-fns. 这生成了一个node_modules文件夹和一个package.json文件。
不完全确定此后如何继续。有哪些必要的步骤?
我只是使用
<script src="{% static 'node_modules/date-fns' %}"></script>
Run Code Online (Sandbox Code Playgroud)
在我的 base.html 文件中?
我正在尝试使用 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) 我正在努力使django压缩工作,但我相信它不起作用因为我的{% static %}使用.
我的模板是(我正在使用pyjade,但无关紧要):
- load staticfiles
- load compress
| {% compress css %}
link(rel="stylesheet", href="{% static 'less/bootstrap.css' %} ")
link(rel="stylesheet", href="{% static 'timepicker/css/bootstrap-timepicker.min.css'%}")
link(rel="stylesheet", href="{% static 'leaflet/addons/locatecontrol/L.Control.Locate.css' %} ")
link(rel="stylesheet", href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css")
link(href='//api.tiles.mapbox.com/mapbox.js/v1.3.1/mapbox.css', rel='stylesheet')
| {% endcompress %}
Run Code Online (Sandbox Code Playgroud)
还是我的settings.py的一部分:
PROJECT_DIR = os.path.dirname(os.path.realpath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, '../static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'media'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles.finders.DefaultStorageFinder',
'compressor.finders.CompressorFinder',
)
COMPRESS_URL = STATIC_URL
COMPRESS_ROOT = STATIC_ROOT
COMPRESS_STORAGE = "staticfiles.storage.StaticFileStorage"
INSTALLED_APPS = (....,'compressor',....)
Run Code Online (Sandbox Code Playgroud)
即使我$ …
django ×10
django-static ×10
python ×2
apache ×1
debugging ×1
mod-python ×1
static ×1
static-files ×1