Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django media URLs in CSS files

In django templates, it's common to do the following:

<img src="{{ MEDIA_URL }}/img/someImage.jpg"> 

How would you accomplish this in a CSS file which is not served as a template?

.someClass {     /* can't do this this */     background: url("{{ MEDIA_URL }}/img/someImage.jpg");             /* either this */     background: url("http://media.domain.com/img/someImage.jpg");     /* or this */     background: url("/django_static_media/img/someImage.jpg");     /* can't do both... what to do? */ } 

I need the ability to serve my files either from the media subdomain, or during offline work and serve them directly as a django static view. But CSS files are a problem since they are not processed as templates and I cannot use the MEDIA_URL context variable.

What's the solution?

Edit: I should note that the problem arises since my static media files are in fact located on a separate media sub-domain, thus negating the use of relative paths. Got it, thanks!

like image 501
Yuval Adam Avatar asked Jun 21 '09 13:06

Yuval Adam


People also ask

Does Django work with CSS?

Thanks to django_compressor, django-css will also version and compress linked and inline javascript or CSS into a single cached file. These cached files will get served through whatever frontend server you use for serving static files, because serving static files through Django is just silly.

How do I serve CSS files in Django?

Configuring static files Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS . In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE . Store your static files in a folder called static in your app.

How do I serve Django media files in production?

Unfortunately, the Django development server doesn't serve media files by default. Fortunately, there's a very simple workaround: You can add the media root as a static path to the ROOT_URLCONF in your project-level URLs.


2 Answers

Where is your css file served from? This usually isn't a problem as a common media structure such as:

media/     images/     css/     js/ 

(or similar) allows for relative file paths for images, eg:

background: url('../images/foo.png'); 

If you're not prepared to change your media folder structure to accommodate relative file paths, you may have no alternative but to overwrite css declarations from within the template, using a secondary css file when offline:

{% if DEBUG %}     <link rel="stylesheet" href="{{ MEDIA_URL }}css/offline-mode.css" /> {% endif %} 

Of course the first option is much tidier.

like image 146
ozan Avatar answered Oct 22 '22 19:10

ozan


Sorry, you won't like the answer.

I've got the same problem:

There is no easy way to do this with static-served CSS files.

What I do:

  • debug server, work locally, media served locally
  • production server is hosted out somewhere commercial w/media on Amazon S3
  • settings.py file auto sets MEDIA_URL (DEBUG, etc.) via hostname check (to differentiate production vs. local/home/debug)
  • HTML files all have css links with {{MEDIA_URL}} (+RequestContext contexts for views)
  • I like absolute path names, so an "update_s3" script: (1) alters each css file is temporarily to fix 'url("/media' to 'url("s3.mydomain.com/media' and (2) updates/uploads my /media directory to Amazon S3

I then go to production and do an svn update & touch the WSGI file & validate

like image 42
joej Avatar answered Oct 22 '22 18:10

joej