Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 656779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:45:05+00:00 2026-05-13T22:45:05+00:00

I’m trying to add .css and .js file in my HTML template files that

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:45:06+00:00Added an answer on May 13, 2026 at 10:45 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 344k
  • Answers 344k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The rel=canonical link element helps search engines to determine the… May 14, 2026 at 5:45 am
  • Editorial Team
    Editorial Team added an answer This sounds like a very bad thing to do! you… May 14, 2026 at 5:45 am
  • Editorial Team
    Editorial Team added an answer Have a look at T4 (Text Template Transformation Toolkit). <#@… May 14, 2026 at 5:45 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.