如何在Django项目中添加图像

用户名

我在Django项目中添加图片时遇到问题,我的项目是

my_app
  manage.py
  my_app 
    urls.py 
    views.py 
    settings.py 
    templates
     home.html
    static
     img
       image1.png

settings.py

默认情况下全部设置

STATIC_URL = '/static/'

home.html我的代码中

{% load staticfiles %}

<img src="{% static 'img/image1.png' %}" />

但无法加载图片,找不到页面

127.0.0.1:1000/static/img/openicon2.png

Page not found (404)
Request Method:     GET
Request URL:    127.0.0.1:1000/static/img/openicon2.png

有人能帮我吗 ?也许我需要修改urls.py

塞尔吉克

以我的经验,我在settings.py中完成了以下操作,一切正常

import os.path,sys
CURRENT_DIR = os.path.dirname(__file__).replace('\\','/')
PROJECT_ROOT = os.path.abspath(os.path.join(CURRENT_DIR, os.pardir))

# other codes and stuff

我添加了一个名为deveop的变量,我对其进行了更改,具体取决于我的应用程序在开发服务器还是apache上运行

DEVELOP = True

# other codes and stuff

if DEVELOP == False:
    MEDIA_ROOT = os.path.join(PROJECT_ROOT,'media')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
if DEVELOP == False:
    STATIC_ROOT = os.path.join(PROJECT_ROOT,'static')

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT,'static'),
    # 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.
)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在SBT项目中添加示例?

来自分类Dev

如何在gradle项目中添加JAR?

来自分类Dev

如何在我的项目中添加梨

来自分类Dev

如何在laravel项目中添加OwlCarousel?

来自分类Dev

如何在Xamarin Forms的列表视图中的多个项目中添加图像单元格

来自分类Dev

如何在Xamarin Forms的列表视图中的多个项目中添加图像单元格

来自分类Dev

如何在Django电子商务项目中将Paypal添加到Payment中

来自分类Dev

如何在我的Django项目中添加第二个表单?

来自分类Dev

如何在Xcode项目中将项目添加为模块

来自分类Dev

如何在单个项目中添加数组项目?

来自分类Dev

如何从项目中的文件夹向collectionview添加图像?

来自分类Dev

如何在我的项目中包含图像作为资源?

来自分类Dev

如何在Android项目中选择多个图像/视频?

来自分类Dev

如何在Openshift托管的项目中存储图像?

来自分类Dev

如何在Android项目中选择多个图像/视频?

来自分类Dev

如何在简单的Java项目中加载图像?

来自分类Dev

在Django项目中添加React

来自分类Dev

如何在 Django 项目中使用 django-trix?

来自分类Dev

如何在我的Leinigen项目中添加本地库?

来自分类Dev

如何在Android Studio项目中添加Flurry?

来自分类Dev

如何在Composer添加的Behat项目中包含库/ API?

来自分类Dev

如何在MVC 5项目中添加jQueryUI库?

来自分类Dev

如何在PyCharm中将RDKit添加到项目中?

来自分类Dev

如何在现有项目中添加可可足类?

来自分类Dev

如何在项目中添加正确的protobuf库?

来自分类Dev

如何在Xcode项目中添加声音效果?

来自分类Dev

如何在NodeJs项目中添加构建脚本和测试

来自分类Dev

如何在旧的角度项目中添加@ angular / material?

来自分类Dev

如何在我的gradle项目中添加JAR?

Related 相关文章

  1. 1

    如何在SBT项目中添加示例?

  2. 2

    如何在gradle项目中添加JAR?

  3. 3

    如何在我的项目中添加梨

  4. 4

    如何在laravel项目中添加OwlCarousel?

  5. 5

    如何在Xamarin Forms的列表视图中的多个项目中添加图像单元格

  6. 6

    如何在Xamarin Forms的列表视图中的多个项目中添加图像单元格

  7. 7

    如何在Django电子商务项目中将Paypal添加到Payment中

  8. 8

    如何在我的Django项目中添加第二个表单?

  9. 9

    如何在Xcode项目中将项目添加为模块

  10. 10

    如何在单个项目中添加数组项目?

  11. 11

    如何从项目中的文件夹向collectionview添加图像?

  12. 12

    如何在我的项目中包含图像作为资源?

  13. 13

    如何在Android项目中选择多个图像/视频?

  14. 14

    如何在Openshift托管的项目中存储图像?

  15. 15

    如何在Android项目中选择多个图像/视频?

  16. 16

    如何在简单的Java项目中加载图像?

  17. 17

    在Django项目中添加React

  18. 18

    如何在 Django 项目中使用 django-trix?

  19. 19

    如何在我的Leinigen项目中添加本地库?

  20. 20

    如何在Android Studio项目中添加Flurry?

  21. 21

    如何在Composer添加的Behat项目中包含库/ API?

  22. 22

    如何在MVC 5项目中添加jQueryUI库?

  23. 23

    如何在PyCharm中将RDKit添加到项目中?

  24. 24

    如何在现有项目中添加可可足类?

  25. 25

    如何在项目中添加正确的protobuf库?

  26. 26

    如何在Xcode项目中添加声音效果?

  27. 27

    如何在NodeJs项目中添加构建脚本和测试

  28. 28

    如何在旧的角度项目中添加@ angular / material?

  29. 29

    如何在我的gradle项目中添加JAR?

热门标签

归档