如何在Django项目中添加图片

3

我在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文件?


这个问题已经被问了至少200次,请在页面右上方的搜索中查询。您是否阅读了文档,它实际上包含了您的答案?可能是Confusion in Django admin, static and media files和许多其他问题的重复。 - Hedde van der Heide
我阅读了文档,并按照 https://docs.djangoproject.com/en/1.5/howto/static-files/ 中的步骤进行操作,同时我还在页面右上方进行了搜索,但是仍然没有找到任何有用的信息。 - user2746078
你所参考的文档是针对 生产环境 应用程序的,其中 服务器 提供静态内容。你显然处于本地开发(DEBUG模式)中,Django提供这些文件。看一下我链接的文档,你就能找到答案了。 - Hedde van der Heide
您正在请求openicon2.png,而您的目录中只有image1.png,可能这就是问题所在。 - Burhan Khalid
我尝试了https://docs.djangoproject.com/en/1.2/howto/static-files/#limiting-use-to-debug-true,并且出现错误 页面未找到(404) 请求方法:GET 请求URL:http://127.0.0.1:1000/myapp/site_media/image.png“/path/to/media\image.png”不存在。 - user2746078
在我的应用程序中,我添加了路径/到/媒体/图像.png。 - user2746078
2个回答

1

根据我的经验,在我的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.
)

еҪ“е·Із»ҸеӯҳеңЁDEBUGж—¶пјҢиҜ·дёҚиҰҒеҲӣе»әж–°зҡ„и®ҫзҪ®DEVELOPгҖӮжӮЁзҡ„STATICFILES_DIRSдёҚеә”еҢ…еҗ«STATIC_ROOTгҖӮ - Burhan Khalid

1

只需添加,

在settings.py文件中:

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

在项目的urls.py文件中:
from django.conf.urls.static import static
from my_app import settings

urlpatterns = [.....]+static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)

然后您可以动态添加图像!在模板中:
<img ... src="{{key.image.url}}" ... >

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接