user639998
user639998

Reputation: 23

Django external css file problem

For a couple of days now I have been trying to setup my django project to run my html-template with an external css-file. So far, no succes....

I have installed staticfiles ( Im using django 1.2.4.) and put the 'staticfiles' in INSTALLED_APPS within settings.py and added the following code:

STATIC_ROOT=os.path.join(os.path.abspath(os.path.dirname(file)), "static")

STATIC_URL='/static/'

My css-file is located under /static/css/stylesheet.css

My html-template has the link

link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/stylesheet"

After running the server, the page loads just fine. However django cant find my stylesheet...

What am I doing wrong here?

Upvotes: 0

Views: 2667

Answers (3)

j_syk
j_syk

Reputation: 6631

The static root and url doesn't actually host the files. The static serve option (in the urls.py) mentioned previously is a good option for development and learning, but if you move to a deployment server you should use the static hosting provided by your webserver.

The way the static folders is intended to work is that you add the path locations for each app, project, etc to the static directories setting in settings.py. Then, when you run the command "django-admin.py collectstatic" django pulls all of your directories into your static root. After the first time you run collectstatic, only files that have changed will be copied again. This consolidates multiple static directories into one common place.

Static files documentation

Upvotes: 1

guerrerocarlos
guerrerocarlos

Reputation: 1424

I would recommend you to just use a django.views.static.serve instance like this in the url.py file:

(r'^(?P<path>.*)$', 'django.views.static.serve',{'document_root': '/path/to/css/'}),

Upvotes: 0

Sam Dolan
Sam Dolan

Reputation: 32542

You need to pass the RequestContext to the view, so it will run through the staticfiles' CONTEXT_PROCESSORS (which includes the STATIC_URL variable).

from django.template.context import RequestContext

context = {'my_other_context': 1}
render_to_response('your_template.html',
                   context_instance=RequestContext(request, context))

Upvotes: 0

Related Questions