3

I'm trying to add .css and .js file in my HTML template files that made for Django. I have followed the official doc, so my configurations set to:

urls.py

urlpatterns = patterns('',
            (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
                {'document_root': settings.STATIC_DOC_ROOT}),

settings.py

STATIC_DOC_ROOT = ''/myfirstapp/templates/seminar_form/' 

But still I couldn't get it right, those .css .js and image files are still doesn't load in my HTML,but those files can access (perfectly viewable) from hitting the URL on browser like this:

http://127.0.0.1:8000/site_media/images/calendar.png 

Template code

<img src="{{ site_media }}images/calendar.png"> 
1
  • How do you link to them in the template? Can you show an example? Commented Mar 5, 2010 at 8:57

3 Answers 3

9

If you can view the files by hitting the URL directly in your browser, then I'd guess you've got your MEDIA_URL settings wrong, or there's something wrong in your template code. What URL is your HTML referencing for your CSS/JS/Images?

Make sure you're passing through your MEDIA_URL so it's available in your template's context, which you do by wrapping up the request passed to your view functions in a RequestContext, like this:

def some_view(request):
    # ...
    return render_to_response('my_template.html',
                              my_data_dictionary,
                              context_instance=RequestContext(request))

Provided you've got your MEDIA_URL 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:

<img src="{{ MEDIA_URL }}relative/path/to/media.jpg" alt="Foobar" />

After edit to show template code:

From your template code, it looks like you're trying to reference files rooted at a template variable called site_media, which probably doesn't exist.

You need to put something like this in your settings.py:

MEDIA_URL = u'/site_media/'

Then change your template code to something like this:

<img src="{{ MEDIA_URL }}images/calendar.png">

and make sure you're passing MEDIA_URL to your template from your view function.

After comment asking for clarification about RequestContext:

The online Django book has some helpful (though currently lacking in some parts) documentation about RequestContext. Personally, I use the render_to decorator from django-annoying to avoid having to think about it. In place of my earlier sample view code, you can just do this:

from annoying import render_to

@render_to('my_template.html')
def some_view(request):
    ...
    return my_data_dictionary

Essentially, you just decorate your view function, passing in the template you want rendered, and then just return a dict containing the extra context variables you want set (i.e. context variables in addition to those that are set for you by RequestContext, such as MEDIA_URL).

This approach obviously doesn't work if your view might use different templates depending on some condition, but there are simple ways around that:

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))

Can be rewritten as:

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

Which seems clearer to me, at least.

Sign up to request clarification or add additional context in comments.

4 Comments

I really didn't get what you have said, there's nothing mentioned about the MEDIA URL in that official doc, please could you explain more further.
@MMRUser - yes there is (I even linked to the docs for you when I first mentioned MEDIA_URL).
@MMRUser - I've edited my answer now you've posted your template code - there's a bit more help about this problem available at arthurkoziel.com/2008/09/02/handling-static-files-django. If you've read that and are still stuck, add a comment explaining how and I'll try to help!
I made the configurations that you have mentioned,and it's working just fine thanks for your help.By the way If you can please explain more about Django template's RequestContext.
2

simple explanation about image loading, I've found:

in settings.py:

from os import path

#media files are in <project folder>/static-media
MEDIA_ROOT = path.join(path.abspath(path.dirname(__file__)), 'static-media')

in urls.py:

from django.conf import settings

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

usage in html:

<!--image location -  <project folder>/static-media/images/calendar.png-->
<img src="/media/images/calendar.png">

1 Comment

about MEDIA_ROOT: if in urlpatterns we write {'document_root': 'static-media'}, then there is no need to write code MEDIA_ROOT=path.join(... in settings.py
2

What has worked for me is, adding the static files path to the STATICFILES_DIRS. IN development environment this seems a better solution.

 STATICFILES_DIRS = (
   '/path/to/your/static/files/',
 )

ex:

STATICFILES_DIRS = (
   '/home/chathuranga/App/templates/',
)

Hope this helps.

1 Comment

A great answer any way have to try this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.