I'm just starting out with Django, using PyDev in AptanaStudio3. I used the helpful Django tutorials to build the backbone of my simple project. Now I am trying to use css for some basic formatting and coloring, but have spent quite a while struggling.
The html page is still showing up fine. It just isn't grabbing the css (404). I know this from the console:
[20/Mar/2013 12:41:51] "GET /signup/ HTTP/1.1" 200 306
[20/Mar/2013 12:41:51] "GET /signup/static/signup.css HTTP/1.1" 404 2750
My file structure:
Learning
- Learning
--- templates
----- 404.html
----- 500.html
--- _init.py
--- settings.py
--- urls.py
--- wsgi.py
- signup
--- static
----- signup.css
--- templates
----- signup
------- detail.html
------- index.html
------- results.html
----- __init__.py
----- admin.py
----- models.py
----- tests.py
----- urls.py
----- view.py
--- manage.py
--- sqlite.db
--- python
In settings:
STATIC_ROOT = ''
STATIC_URL = 'static/'
STATICFILES_DIRS = ('')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
In my html template:
<html>
<head>
<title>User List</title>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}signup.css" />
...
In views.py:
def ip_address_processor(request):
return {'ip_address': request.META['REMOTE_ADDR']}
def index(request):
user_list = User.objects.order_by('name')[:5]
template = loader.get_template('signup/index.html')
context = RequestContext(
request,
{'user_list': user_list,},
[ip_address_processor])
return HttpResponse(template.render(context))
Any insights?
./manage.py collectstatic? Django needs all static files to be collected in a directory to serve them using the test server. Also, be sure to set yourSTATIC_ROOTsetting to an absolute and writable path, so that the files can effectively be collected. For more information, see the Django documentation on static files. Also, remember that in production environment, static files should be served directly by the HTTP server./signup/static/signup.css, while yourSTATIC_URLis/static/. You need to make that path absolute, and not relative to your page. Either change tohref="/{{ STATIC_URL }}signup.css", or use the{% static 'signup.css' %}template tag provided by thestaticfileslibrary (better !).