1

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)?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.