django - Loading images in a static file -


so have completed django tutorial, play around some, , feel have understanding of django. problem facing when try display static page landing page, cannot seem images load.

first off have tried 2 different methods of displaying static landing page. first:

# main app urls.py     django.conf.urls import patterns, include, url      django.contrib import admin     admin.autodiscover()      urlpatterns = patterns('',         url(r'^$', templateview.as_view(template_name="landing/index.html")),         url(r'^polls/', include('polls.urls', namespace="polls")),         url(r'^admin/', include(admin.site.urls)),     )   

this main app's urls.py , use templateview display static index.html located @ 'my_app/templates/landing/index.html'.

this , until try add static image index.html file. no matter put image file cannot seem display. in index.html template have tried different methods of using static trying different direct paths without need embedded python code. how supposed display images , should located?

the second method found worked displaying static page (not image) create new app called landing , have display static page urls.py in same manner(using templateview). method better? still had same problems in displaying image within static page first method, makes me think has templateview.

am doing wrong? options? using django 1.5.1.

thanks in advance!

it bit awkward serve statics files django. because have conception static files such images must in domain or server performance/security reasons.

to serve static content django this:

# urls.py # add lines @ end  django.conf.urls.static import static django.conf import settings django.contrib.staticfiles.urls import staticfiles_urlpatterns urlpatterns += staticfiles_urlpatterns() urlpatterns += static(settings.media_url, document_root=settings.media_root) 

then in settings.py define directory images/css/js , similar static contents are:

# settings.py  import os settings_dir = os.path.dirname(__file__) project_root = os.path.abspath(os.path.dirname(settings_dir))  static_url = '/static/'  staticfiles_dirs = (     # assumes static contents in <your_project>/static/     os.path.join(project_root, 'static/'),     )  media_root = os.path.join(project_root, 'media/') media_url = '/media/' 

note: may consider images media , place them in media/ folder.

that should static files served app. reference them href="/static/xxxx.jpg" or href="/media/xxxx.jpg".

hope helps!


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -