Reputation: 12466
I put the image("hi.png") and css file in /home/user1/djangoblog/blog/static
In templates i have <img src="{{ STATIC_URL }}images/hi.png" />
I edited the settings.py =>
STATIC_ROOT = ''
STATIC_URL = "/static/"
STATICFILES_DIRS = (
"/home/user1/djangoblog/blog/static/",)
The web page source returns =>
<div><img src="/static/images/a.jpg" /></div>
If the views.py => The image is not displayed on the web page.
now = datetime.datetime.now()
#return render_to_response('current_datetime.html', {"foo": now}, context_instance=RequestContext(request))
t = loader.get_template('current_datetime.html')
c = Context({'foo': now})
return HttpResponse(t.render(c))
But if the views.py => it displays the images on the web page.
return render_to_response('current_datetime.html', {"foo": now}, context_instance=RequestContext(request))
It looks like it's a problem with HttpResponse or RequestContext. How can i fix this problem with HttpResponse or RequestContext?
Upvotes: 2
Views: 1974
Reputation: 239400
You need to read the staticfiles docs more carefully.
You specify the absolute path to your static directory with STATIC_ROOT
and the URL that directory can be accessed at through a browser via STATIC_URL
. Your main static directory should never go in STATICFILES_DIRS
. That setting is only to allow you to specify additional directories that should be processed while running the collectstatic
management command.
In general, you never use your actual static directory. In development Django serves files from each app's static
directory and any directories in STATICFILES_DIRS
(excluding the main static
directory). In production, you run the collectstatic
management command and then setup your main webserver (Apache, Nginx, etc) to serve files from STATIC_ROOT
. Django never serves anything from the main static
directory (STATIC_ROOT
).
Upvotes: 3
Reputation: 21022
I suspect the problem is that you don't have the staticfiles context processor in your settings file. That's the reason that /static/
isn't being inserted where you've put {{ STATIC_URL }}
.
It's as simple as putting
'django.core.context_processors.static',
Into the list of context processors (TEMPLATE_CONTEXT_PROCESSORS
) in your settings.py file.
I added this in TEMPLATE_CONTEXT_PROCESSORS but still that didn't work. – guru 3 hours ago
Adding 'django.core.context_processors.static'
to the list of context processors did fix your problem because as you can now see, it is inserting the value for STATIC_URL
wherever it sees {{ STATIC_URL }}
in your templates. The error is that you've changed the value of STATIC_URL
to "/home/user1/djangoblog/blog/static/"
probably because you've been confused by some of the answers here.
Change the value of STATIC_URL
back to '/static/'
so that line should read
STATIC_URL = '/static/'
That will fix your problem.
Django templates simply put whatever is in the variable into the template. They don't care what it is or what it represents. If the value of STATIC_URL is /home/user1/djangoblog/blog/static/
then it dutifully inserts /home/user1/djangoblog/blog/static/
wherever it sees {{ STATIC_URL }}
which is why you are seeing
<img src="/home/user1/djangoblog/blog/static/images/a.jpg" />
whenever it renders the template containing <img src="{{ STATIC_URL }}images/a.png" />
Upvotes: 3