Question

I have a site, where users can upload images. And it works fine in django's own server. But I am using apache to serve both my app and statics. And when I upload any image, I can't see the pics. Only when I run collectstatic I can see them. Do I always have to run collectstatic? Is there any better way to do this?

My settings.py snippet:

MEDIA_ROOT = 'C:/Users/Robin/web/leo/static'

STATIC_ROOT = 'C:/Users/Robin/static/'

STATICFILES_DIRS = (
    "C:/Users/Robin/web/leo/static",

Apache http.cont snippet:

AliasMatch ^/([^/]*\.css) C:/Users/Robin/static/styles/$1

Alias /static/ C:/Users/Robin/static/

<Directory C:/Users/Robin/static>
Order deny,allow
Allow from all
</Directory>

WSGIScriptAlias / C:/Users/Robin/web/leo/leo/wsgi.py

<Directory C:/Users/Robin/web/leo/leo>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
Was it helpful?

Solution

You should separate media files from static files. Media are for user-uploaded files, and static are for developer-provided files (usually styles and scripts). MEDIA_ROOT and STATIC_ROOT should point to different directories. You run collectstatic to copy all static files from Your source code and various apps (also third-party) to STATIC_ROOT, so Apache can serve them. You don't need to run collectstatic and anything special to serve Your media files.

Example config:

Alias /static/ C:/Users/Robin/static/
Alias /media/ C:/Users/Robin/media/

<Directory C:/Users/Robin/static>
    Order deny,allow
    Allow from all
</Directory>

<Directory C:/Users/Robin/media>
    Order deny,allow
    Allow from all
</Directory>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top