Image not displaying in Django 1.8 template -
i want display images static folder inside django app called index
. shows icon in browser means cannot find image in path specified in <img src=""/>
. here's directory structure:
|-- db.sqlite3 |-- images | |-- __init__.py | |-- __init__.pyc | |-- settings.py | |-- settings.pyc | |-- urls.py | |-- urls.pyc | |-- wsgi.py | `-- wsgi.pyc |-- index | |-- admin.py | |-- admin.pyc | |-- __init__.py | |-- __init__.pyc | |-- migrations | | |-- __init__.py | | `-- __init__.pyc | |-- models.py | |-- models.pyc | |-- static | | `-- index | | `-- file2 | |-- templates | | `-- show.html | |-- tests.py | |-- urls.py | |-- urls.pyc | |-- views.py | `-- views.pyc `-- manage.py
views.py
def show(request): return render(request, 'show.html', {})
urls.py
from django.conf.urls import url django.conf import settings django.conf.urls.static import static index import views urlpatterns = [ url(r'^show$', views.show), ] + static(settings.static_url, document_root=settings.static_root)
show.html
{% load staticfiles %} <img src="{% static "index/file2" %}" alt="test_image"/>
i have tried follow this not doing properly. appreciated.
after lot of trials, got combination of answers stackoverflow. not straightforward. phew! i'm happy , smiling though. i've setup django 1.8 on windows apache , mod_wsgi (xampp). 'vrsite' site. 'vrapp' app. image showed vrsite> vrsite > vrapp > static > vrapp >23-oracle.jpg steps getting image displayed below: 1. directory structure: c:...\myvirtualwebenv27_1\xampp\htdocs\vrsite\manage.py <---root
vrsite
-- manage.py -- static --media -- static_dirs -- css -- img -- 23-oracle.jpg -- js -- static_root -- admin -- css -- img -- js -- css -- img -- 23-oracle.jpg -- vrapp --23-oracle.jpg -- vrsite -- __init__.py -- settings.py -- urls.py -- wsgi.py -- vrapp -- migrations -- static -- vrapp -- 23-oracle.jpg -- templates -- index.html -- __init.py__ -- admin.py -- models.py -- tests.py -- views.py
2. settings.py
import os base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) installed_apps = ( 'django.contrib.staticfiles', 'vrsite.vrapp', ) root_urlconf = 'vrsite.urls' wsgi_application = 'vrsite.wsgi.application' static_url = '/static/' static_root = os.path.join(base_dir, 'static', 'static_root') #static_root = 'static' staticfiles_dirs = ( os.path.join(base_dir, 'static', 'static_dirs'), ) media_root = os.path.join(base_dir, 'static', 'media') media_url = '/media/'
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin django.conf import settings django.conf.urls.static import static urlpatterns = [ url(r'^$', 'vrsite.vrapp.views.home'), ] + static(settings.static_url, document_root=settings.static_root)
views.py
from django.shortcuts import render
from django.shortcuts import render_to_response
def home(request): return render_to_response('index.html')
index.html
< body >
{% load staticfiles %} <img src="{% static "vrapp/23-oracle.jpg" %}" alt = "my image"/>
< /body >
run collect static follows
(myvirtualwebenv27_1) c:...\myvirtualwebenv27_1\xampp\htdocs\vrsite> c:...\myvirtualwebenv27_1\scripts\python manage.py collectstatic
you have requested collect static files @ destination location specified in settings:
c:\...\myvirtualwebenv27_1\xampp\htdocs\vrsite\static\static_root
this overwrite existing files! sure want this?
type 'yes' continue, or 'no' cancel: yes copying c:...\myvirtualwebenv27_1\xampp\htdocs\vrsite\vrsite\vrapp\static\vrapp\23-oracle.jpg'
1 static file copied 'c:...\myvirtualwebenv27_1\xampp\htdocs\vrsite\static\static_root', 64 unmodified.
Comments
Post a Comment