Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Django项目中添加图像_Python_Django - Fatal编程技术网

Python 如何在Django项目中添加图像

Python 如何在Django项目中添加图像,python,django,Python,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 staticf

我在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' %}" />

有人能帮我吗?也许我需要修改
url.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的变量,并根据我的应用程序是在development server还是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.
)
只需添加,

在settings.py中:

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
在项目的URL.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}}" ... >


此问题已被询问至少200次,请查询此页面右上角的搜索。你读过那本书吗?它几乎能给出你的答案?可能的重复和许多其他我阅读了文档,并遵循了中的步骤,同时我检查了本页右上角的搜索,没有任何帮助???您所指的文档是针对生产应用程序的,其中服务器提供静态内容。显然,您正处于本地开发(调试模式),Django在其中提供这些文件。看看我链接的文档,这就是你的答案。你正在请求
openicon2.png
,而你的目录只有
image1.png
,也许这就是问题所在。我试过了,但找不到错误页(404)请求方法:获取请求URL:“/path/to/media\image.png”不存在当已存在调试时,不要创建新设置
develope
。您的
STATICFILES\u DIRS
不应包括
STATIC\u ROOT
<img ... src="{{key.image.url}}" ... >