热门标签 | 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


推荐阅读
  • Ubuntu安装常用软件详细步骤
    目录1.GoogleChrome浏览器2.搜狗拼音输入法3.Pycharm4.Clion5.其他软件1.GoogleChrome浏览器通过直接下载安装GoogleChro ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • node.jsurlsearchparamsAPI哎哎哎 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 深度学习中的Vision Transformer (ViT)详解
    本文详细介绍了深度学习中的Vision Transformer (ViT)方法。首先介绍了相关工作和ViT的基本原理,包括图像块嵌入、可学习的嵌入、位置嵌入和Transformer编码器等。接着讨论了ViT的张量维度变化、归纳偏置与混合架构、微调及更高分辨率等方面。最后给出了实验结果和相关代码的链接。本文的研究表明,对于CV任务,直接应用纯Transformer架构于图像块序列是可行的,无需依赖于卷积网络。 ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
  • 本文介绍了某点评网的搜索策略,包括名称和地址的匹配策略,模糊匹配的方法以及不同口音和拼音的近似发音。同时提供了一些例子来说明这些策略的应用。 ... [详细]
  • 程序员如何选择机械键盘轴体?红轴和茶轴对比
    本文介绍了程序员如何选择机械键盘轴体,特别是红轴和茶轴的对比。同时还介绍了U盘安装Linux镜像的步骤,以及在Linux系统中安装软件的命令行操作。此外,还介绍了nodejs和npm的安装方法,以及在VSCode中安装和配置常用插件的方法。最后,还介绍了如何在GitHub上配置SSH密钥和git的基本配置。 ... [详细]
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社区 版权所有