Ed.
Ed.

Reputation: 4577

Serving static media in django application

I notice that when I reference my java scripts and static image files from my templates, they show up in development, but not from the production server. From development, I access them as such:

<img src="/my_proj/media/css/images/collapsed.png" />

but from production, I have to remove the project directory:

<img src="/media/css/images/collapsed.png" />

I'm assuming I'm doing something wrong with regard to serving static media.

I'm caught between a number of seemingly different options for serving static media in Django. On one hand, it's been recommended that I use django-staticfiles to serve media. On the other I see reference to STATIC_ROOT and STATIC_URL in the documentation (with caveats about use in production).

I have small .png files of "plus" and "minus" symbols for use in some of my jQuery scripts. In addition, the scripts themselves need to be referenced.

1) Am I correctly categorizing scripts and site images as static media?
2) What is the best method to access this media (from production)?

Upvotes: 1

Views: 719

Answers (2)

Tiago
Tiago

Reputation: 9557

You shouldn't keep the URLs hardcoded that way.

If you're running thedev version of Django, you should check this doc.

If you're not, for simplicity, you can use {{ MEDIA_URL }} and configure that variable in your settings for dev and for production.

<img src="{{ MEDIA_URL }}/media/css..." />

Keep in mind that you'll have to use RequestContext, see more here on docs here and here.

Also, you should server all your static files directly trough a proper webserver. You can configure apache or nginx to do that.

Upvotes: 3

Daniel Roseman
Daniel Roseman

Reputation: 599450

Sigh. There is only one way to serve static media in production, and that is to get the webserver - eg Apache - to do it. Django-staticfiles is for development only, and this is clearly stated throughout the documentation.

Upvotes: 1

Related Questions