AJNinja
AJNinja

Reputation: 140

How to make my css files to work in django

I am new to django but have some relative skills in python. I just started building a project in django and so far I have been able to create an app within my project. Also, I have been able to link the index.html file of my website to django and can successfully view it after running the server on the command prompt. But here are my questions;

  1. How do i make the css, img & js files to work with my index.html?

Thanks to you guys for the responses so far, but still the index.html page loads on the localhost address but it doesn't apply the img,css and js scripts. It only loads the html. Although i have tried to use the method stated by the 2nd person that responded to my post but still no luck.

Upvotes: 3

Views: 377

Answers (2)

AJNinja
AJNinja

Reputation: 140

Thanks to all those that responded to my question, my static files are now active. I had to add RequestContext in my project view to make the {{STATIC_URL}} in the html file active.

def index(request): return render_to_response('Calculator/index.html', context_instance=RequestContext(request))

Upvotes: 0

Thomas Schwärzl
Thomas Schwärzl

Reputation: 9917

Just a small demo for very basic css/img usage with the development-server: python manage.py runserver. You should not use this for production. For the user input/output you should have a look at the tutorial.

filestructure

project
  |- static
  |   |- css
  |   |   |- stuff.css
  |- media
  |   |- images
  |   |   |- love.jpg
  |- templates
  |- urls.py
  |- __init__.py
  |- manage.py
  |- settings.py

base.html

<html>
<head>
   <link href="{{ STATIC_URL }}css/stuff.css" />
</head>
<body>
   <h1>I like django!</h1>
   <img src="{{ MEDIA_URL }}images/love.jpg" />
</body>
</html>

settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.core.context_processors.request',
    'django.contrib.messages.context_processors.messages',
)

STATIC_URL = '/static/'
MEDIA_URL = '/media/'

urls.py

# example windows
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': 'C:/python25/lib/site-packages/project/media/', 'show_indexes': True}),
# example *ix
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': '/usr/lib/python25/site-packages/project/static/', 'show_indexes': True}),

Upvotes: 1

Related Questions