Django cannot find static images in templates

I reviewed almost all the examples here and in the documentation, and it just doesn't work at all

So in my settings.py file I have

STATIC_ROOT = '/mattr/static/' STATIC_URL = '/mattr/public/' STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder',) TEMPLATE_CONTEXT_PROCESSORS = ('django.core.context_processors.static',) TEMPLATE_DIRS = ('mattr/public', ) 

Basically everything you need to process static files.

In urls.py I have the usual page templates (templates just load) and add this extra line

 urlpatterns += staticfiles_urlpatterns() 

In views.py I have (this is for the main page):

 def home(request): t = get_template('index.html'); html = t.render(RequestContext(request)) return HttpResponse(html) 

And in the index.html template file, I have a line

 <img src="{{ STATIC_URL }}media/images/Mattr1.png"> 

And yet he never shows images. Even when I try to just go to the image file directly at http://127.0.0.1:8000/mattr/public/media/images/Mattr1.png, it gives me a "Page Error" error. I was a little confused when the path starts, but due to loading the template page, I realized that I have the correct paths.

+2
django django-staticfiles
source share
3 answers

when you talk about static files do the following:

 STATIC_URL = '/static/' #or whatever you want STATICFILES_DIRS = ( '/path/to/static/root/directory/', ) 

Remember that a coma or django admin will not have its own css. It does not need to change anything in urls.py

If you are talking about media, do the following:

 MEDIA_ROOT = '/media/' # or whatever you want MEDIA_URL = '/path/to/media/root/directory' 

and put this below in myproject.urls :

 import settings urlpatterns += patterns('', url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT,}),) 

done!

+1
source share

Try this in your .py settings:

 import os DIRNAME = os.path.dirname(__file__) STATIC_ROOT = os.path.join(DIRNAME, 'static') STATIC_URL = '/static/' 

in your templates:

 {{STATIC_URL}}css/etc... 

You also rewrite your TEMPLATE_CONTEXT_PROCESSORS as follows:

 import django.conf.global_settings as DEFAULT_SETTINGS TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + ( "whatever_your_adding", ) 
0
source share

Using '/' in front of the track means that you are referencing a root directory that I think is not the same. Try to do it.

 STATIC_ROOT = 'mattr/static/' 
0
source share

All Articles