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
I aded 코드
{% 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] 삭제
몇 마디 만하겠습니다