Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you can view the files by hitting the URL directly in your browser, then I'd guess you've got your <a href="http://docs.djangoproject.com/en/dev/ref/settings/#media-url" rel="noreferrer"><code>MEDIA_URL</code></a> settings wrong, or there's something wrong in your template code. What URL is your HTML referencing for your CSS/JS/Images?</p> <p>Make sure you're passing through your <code>MEDIA_URL</code> so it's available in your template's context, which you do by wrapping up the request passed to your view functions in a <code>RequestContext</code>, like this:</p> <pre><code>def some_view(request): # ... return render_to_response('my_template.html', my_data_dictionary, context_instance=RequestContext(request)) </code></pre> <p>Provided you've got your <code>MEDIA_URL</code> setting correct, and you are serving your media correctly (which your question suggests you are) you should be able to access media using things like:</p> <pre><code>&lt;img src="{{ MEDIA_URL }}relative/path/to/media.jpg" alt="Foobar" /&gt; </code></pre> <h3>After edit to show template code:</h3> <p>From your template code, it looks like you're trying to reference files rooted at a template variable called <code>site_media</code>, which probably doesn't exist.</p> <p>You need to put something like this in your <code>settings.py</code>:</p> <pre><code>MEDIA_URL = u'/site_media/' </code></pre> <p>Then change your template code to something like this:</p> <pre><code>&lt;img src="{{ MEDIA_URL }}images/calendar.png"&gt; </code></pre> <p>and make sure you're passing <code>MEDIA_URL</code> to your template from your view function.</p> <h3>After comment asking for clarification about <code>RequestContext</code>:</h3> <p>The online Django book has some helpful (though currently lacking in some parts) <a href="http://www.djangobook.com/en/beta/chapter10/" rel="noreferrer">documentation</a> about <code>RequestContext</code>. Personally, I use the <a href="http://bitbucket.org/offline/django-annoying/wiki/Home" rel="noreferrer"><code>render_to</code></a> decorator from django-annoying to avoid having to think about it. In place of my earlier sample view code, you can just do this:</p> <pre><code>from annoying import render_to @render_to('my_template.html') def some_view(request): ... return my_data_dictionary </code></pre> <p>Essentially, you just decorate your view function, passing in the template you want rendered, and then just return a <code>dict</code> containing the extra context variables you want set (i.e. context variables in addition to those that are set for you by <code>RequestContext</code>, such as <code>MEDIA_URL</code>).</p> <p>This approach obviously doesn't work if your view might use different templates depending on some condition, but there are simple ways around that:</p> <pre><code>def some_view(request, some_var): ... if some_var: return render_to_response('my_template.html', my_data_dictionary, context_instance=RequestContext(request)) else: return render_to_response('my_other_template.html', my_data_dictionary, context_instance=RequestContext(request)) </code></pre> <p>Can be rewritten as:</p> <pre><code>def some_view(request, some_var): ... if some_var: return _some_private_view(request, my_data_dictionary) else: return _some_other_private_view(request, my_data_dictionary) @render_to('my_template.html') def _some_private_view(request, my_data_dictionary): return my_data_dictionary @render_to('my_other_template.html') def _some_private_view(request, my_data_dictionary): return my_data_dictionary </code></pre> <p>Which seems clearer to me, at least.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload