python - How To add images in Django project -
i have problem adding images in django project, project
my_app manage.py my_app urls.py views.py settings.py templates home.html static img image1.png
in settings.py
is set default
static_url = '/static/'
in home.html
aded code
{% load staticfiles %} <img src="{% static 'img/image1.png' %}" />
but can't load images , page not found
127.0.0.1:1000/static/img/openicon2.png page not found (404) request method: request url: 127.0.0.1:1000/static/img/openicon2.png
can me ? maybe need modify urls.py
?
in experience i've done following in settings.py , everythings works ok
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 , stuff
i've added variable named deveop , change depends on if application runs on development server or apache
develop = true # other codes , stuff if develop == false: media_root = os.path.join(project_root,'media') # url handles media served media_root. make sure use # trailing slash. # examples: "http://example.com/media/", "http://media.example.com/" media_url = '/media/' # absolute path directory static files should collected to. # don't put in directory yourself; store static files # in apps' "static/" subdirectories , in staticfiles_dirs. # example: "/var/www/example.com/static/" if develop == false: static_root = os.path.join(project_root,'static') # url prefix 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, "/home/html/static" or "c:/www/django/static". # use forward slashes, on windows. # don't forget use absolute paths, not relative paths. )
Comments
Post a Comment