Reputation: 3277
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?
Upvotes: 1
Views: 331
Reputation: 6243
Your STATIC_URL
is a relative path (static/
), which means that all static files will be searched relative to the current page. Django's test server does not expect this behaviour, and so static URLs will not be matched, triggering the 404 error. You need to make that path absolute, and not relative to your page (/static/
, for example)
When using Django's staticfiles system with the testserver, you need to collect your static files using ./manage.py collectstatic
I also strongly recommend using the {% static 'signup.css' %}
syntax rather than {{ STATIC_URL }}signup.css
. Note that if you use this alternate format, you need to include {% load static %}
somewhere earlier in the file.
Upvotes: 1