Hubro
Hubro

Reputation: 59343

How do I host a Django project's admin static files?

I've heard I should use Apache for serving static files in a production environment. I'm having some problems understanding how I'm supposed to do that though. My project's static URL is /static/, and django.contrib.admin's static path is /static/admin/. Those are two completely separate directories on my server, and I can hardly do this:

Alias /static /path/to/site.com/static
Alias /static/admin /usr/local/.../django/contrib/admin/media

Since they overlap.

How am I supposed to do this? Do I really have to copy the contrib admin static folder into my own?

Upvotes: 1

Views: 2502

Answers (3)

jonmsawyer
jonmsawyer

Reputation: 58

You can reverse the order of the Alias entries and Apache will parse it as intended:

Alias /static/admin /usr/local/.../django/contrib/admin/media
Alias /static /path/to/site.com/static

This is because when Apache loads its configuration, it stores entries from a top down perspective. So it first tries to match /static/admin, then if the URI doesn't match, it then tries to match /static.

Upvotes: 0

zaan
zaan

Reputation: 897

Common solution is using /media/ for admin media static files, so it could be in settings.py

ADMIN_MEDIA_PREFIX = '/media/'

and in virtual host config:

Alias /media /path/to/django/contrib/admin/media/
<Location /media>
    SetHandler None
</Location>

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599630

Firstly, no-one says you have to serve your admin static files from the same base path as the others. You can set ADMIN_MEDIA_PREFIX to whatever you like.

However, surely the easiest thing is just to add a symlink from your static folder to django/contrib/admin/media.

Upvotes: 1

Related Questions