热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Django:静态图像不会加载。-Django:StaticImagewon'tload

Ihavebeenfollowingtheofficialdocumentationtotheletter,tryingsomeoftheadviceforrelate

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()之类的。

3 个解决方案

#1


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_URL:您的服务器将为您的静态文件提供服务的URL路径。这是这样的,当你使用静态模板时,Django知道如何将它倒转。换句话说,它仅仅是一种方便的方式来改变{%静态“…”% }到/静态/ ....
  • 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.
  • STATIC_ROOT:在您的服务器上(或在云中某处),Django将复制您的静态文件,以便您的服务器可以为它们服务。当您运行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).
  • STATICFILES_DIRS:每当运行collectstatic时,任何额外的目录Django都应该查找静态文件。在默认情况下,Django只在每个应用程序的目录中查找一个静态目录(就像模板一样)。

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()

#2


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 :)

详见以下答案:

#3


0  

  1. Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.
  2. 确保django.contrib。staticfiles包含在您的INSTALLED_APPS中。
  3. In your settings file, define STATIC_URL, for example: STATIC_URL = '/static/'
  4. 在您的设置文件中,定义STATIC_URL,例如:STATIC_URL = '/static/'
  5. Hopes you stored images in folder /static/projectname/image.png
  6. 希望您将图像存储在文件夹/静态/projectname/image.png中。
  7. Finally in your html simply add the following
  8. 最后在html中添加以下内容。

My image

图像

please refer django dcumentation

请参阅django dcumentation


推荐阅读
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • 本文详细介绍了GetModuleFileName函数的用法,该函数可以用于获取当前模块所在的路径,方便进行文件操作和读取配置信息。文章通过示例代码和详细的解释,帮助读者理解和使用该函数。同时,还提供了相关的API函数声明和说明。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 禁止程序接收鼠标事件的工具_VNC Viewer for Mac(远程桌面工具)免费版
    VNCViewerforMac是一款运行在Mac平台上的远程桌面工具,vncviewermac版可以帮助您使用Mac的键盘和鼠标来控制远程计算机,操作简 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 本文介绍了三种方法来实现在Win7系统中显示桌面的快捷方式,包括使用任务栏快速启动栏、运行命令和自己创建快捷方式的方法。具体操作步骤详细说明,并提供了保存图标的路径,方便以后使用。 ... [详细]
  • Ihavethefollowingonhtml我在html上有以下内容<html><head><scriptsrc..3003_Tes ... [详细]
author-avatar
王之玉58_913
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有