I have been following the official documentation to the letter, trying some of the advice for related questions on here, and just searching the web in general and I am still having no luck getting one little image to load.
我一直在关注这封信的官方文件,在这里尝试一些相关问题的建议,我只是在网上搜索,我仍然没有得到一个小的图片来加载。
I have an image called 'logo.png' and my project hierarchy looks like this:
我有一个叫“logo”的图片。png和我的项目层级是这样的:
project/
mysite/
app/
__init__.py
admin.py
models.py
urls.py
view.py
mysite/
__init__.py
settings.py
urls.py
views.py
wsgi.py
static/
logo.png
templates/
index.html
calling_image.html
Inside settings.py
I have STATIC_URL = '/static/'
内部设置。py我有STATIC_URL = '/static/'
Inside calling_image.html
I have
calling_image内部。我有 and ran collecstatic
and everything worked! Didn't need to use urlpatterns += staticfiles_urlpatterns()
and whatnot.
解决了:所以我自己解决了这个问题。我在项目/mysite/和放置徽标的范围内创建了一个目录资源。png。然后设置STATICFILES_DIRS = (os.path)。加入(BASE_DIR,“resources”),然后运行collecstatic,一切都成功了!不需要使用urlpatterns += staticfiles_urlpatterns()之类的。
4
It looks like you probably need the variable STATICFILES_DIRS
defined in your settings.py and that should include the location of that static
directory holding logo.png
.
看起来您可能需要在设置中定义的变量STATICFILES_DIRS。py和它应该包括静态目录保持logo.png的位置。
Django only looks for static files inside a static
directory inside each app by default. If you have a static file outside of any app, it won't be picked up by collectstatic
automatically.
Django只在默认情况下在每个应用程序内的静态目录中查找静态文件。如果您在任何应用程序之外都有一个静态文件,那么它不会自动被collectstatic接收。
See https://docs.djangoproject.com/en/1.8/ref/settings/#std:setting-STATICFILES_DIRS
看到https://docs.djangoproject.com/en/1.8/ref/settings/ std:setting-STATICFILES_DIRS
Confusion Surrounding Staticfiles
围绕Staticfiles混乱
There's always a lot of confusion around Django and Static files. I believe this confusion comes from the fact that it doesn't make any sense for Django to handle static files in production, but it seems natural to use it when learning to use Django and thus when initially developing.
Django和静态文件总是有很多混淆。我认为这种混淆来自于这样一个事实,即Django在生产过程中处理静态文件没有任何意义,但是在学习使用Django并在最初开发时使用它似乎是很自然的事情。
The reason it doesn't make any sense for Django to serve static files is that Django is a framework for rendering dynamic responses. "Static" files are not dynamic: they don't change. Thus, it's a huge waste of resources to spin up a Python application, which calls all of the Django machinery, just to find and return a file that has no dynamic content.
对Django提供静态文件没有任何意义的原因是Django是一个用于呈现动态响应的框架。“静态”文件不是动态的:它们不会改变。因此,创建Python应用程序(调用所有Django机器)是一种巨大的资源浪费,只是为了找到并返回一个没有动态内容的文件。
Staticfiles: the job of the webserver
静态文件:网络服务器的工作。
There is something that is really good at serving static files: webservers themselves (such as a Apache, nginx, etc.). That's what they were designed to do. The webserver can run a Python/Django application or it can just locate a file and send it back, so you typically configure a webserver by telling it something like the following (in pseudo-server-speak):
有一些东西确实很好地服务于静态文件:web服务器本身(比如Apache、nginx等)。这就是他们设计的目的。webserver可以运行Python/Django应用程序,或者它可以找到一个文件并将其发送回来,所以您通常通过告诉它如下(在伪服务器会话中)来配置一个webserver:
When someone accesses the path
/static/
let them access the files in the following directory:/var/www/static/
.当有人访问路径/静态/让他们访问以下目录中的文件:/ var/www/static/。
When someone accesses the path
/
, spin up this Python application that lives over here:/var/www/django-app
.当有人访问路径/时,将此Python应用程序转到这里:/var/www/django-app。
Django Static Files Tools
Django静态文件的工具
As a result, Django comes with some helper tools that manage static files, so that your actual server can serve them.
因此,Django提供了一些帮助工具来管理静态文件,这样您的实际服务器就可以为它们服务了。
These tools are the following(defined in settings.py
):
这些工具如下(在settings.py中定义):
STATIC_URL
: the URL path where your server will be serving your static files. This is just so that when you use the static
templatetag, that Django knows how to urlreverse
it. In other words, it's merely a convenient way of turning {% static "..." %}
into /static/...
.STATIC_ROOT
: the place on your server (or in the cloud somewhere), to which Django will copy your static files, so that your server can serve them. This copying happens when you run collectstatic
. STATICFILES_DIRS
: any extra directories Django should look for static files whenever you run collectstatic
. By default Django only looks in each app's directory for a static
directory (just like with templates
).Static Files In Development
静态文件的发展
Okay, but that's not so helpful in development where you are probably using Django's runserver
command. You don't have a server running that will server static files.
好吧,但是在开发中,你可能使用Django的runserver命令,这并不是很有用。没有服务器运行服务器静态文件。
Thus, you can ask Django to please also server static files for you in just this one case, because you are developing your application and don't want to run a separate server.
因此,您可以让Django在这一种情况下也为您提供服务器静态文件,因为您正在开发应用程序,并且不想运行单独的服务器。
There is a view that automatically should pick up and serve static files when DEBUG=True
. Alternately, this is also why someone might use the following:
有一个视图,当DEBUG=True时,它会自动获取并服务静态文件。另外,这也是为什么有人可能会使用以下方法:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
0
If I recall, you need to specify STATIC_ROOT = 'static'
in settings.py
and you need to have a URL to direct /static
to your static files. like so.
如果我记得,您需要在设置中指定STATIC_ROOT = 'static'。py和您需要有一个URL来直接/静态到您的静态文件。像这样。
urlpatterns += patterns('',
(r'^static/(?P.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT, 'show_indexes': True
}),
)
This could be a bit outdated but still works for me.
这可能有点过时,但仍然适用于我。
Also, are you trying to get this to work on your dev site with python manage.py runserver
or on a production site?
另外,您是否想让它在python管理的开发站点上工作。py runserver还是在生产站点上?
Update
更新
Here is an example for you main urls.py file.
这里有一个为您提供主要url的示例。py文件。
from django.conf.urls import patterns, include, url
urlpatterns = (
url(r'^admin/', include(admin.site.urls)),
# more urls...
)
#The following enable structural 'static' files while in development mode.
if settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT, 'show_indexes': True
}),
)
patterns
is imported near the top of your urls.py file.
模式是在url的顶部导入的。py文件。
This is where STATIC_ROOT
comes in to play.
这就是STATIC_ROOT的作用所在。
You may also need to run python manage.py collectstatic
in order for your static files to be collected from your various apps and copied into the folder that is STATIC_ROOT
.
您可能还需要运行python管理。为了让静态文件从各种应用程序中收集,并复制到STATIC_ROOT的文件夹中,py collectstatic。
See this answer for a much more in depth explanation :)
详见以下答案:
0
django.contrib.staticfiles
is included in your INSTALLED_APPS
.STATIC_URL = '/static/'
/static/projectname/image.png
please refer django dcumentation
请参阅django dcumentation