0

I have a django web application which create screenshot calling an external python script.

But I'm concerned because every time I run the script I make a ./manage collectstatic to see the screenshots on my application. Soon I could have lots of collectstatic running simultaneously and it seems bad.

How can I have a folder where I can put the screens and then acces it with django without having to load it as a static file?

My current project looks like :

mysite
├── static
│   └── screenshots
|       └── *.png
└── crowlers
    ├── wrapper.py
    └── screenshot_robot.py

/opt/scripts/my_script.sh # launch wrapper.py and collectstatic

wrapper.py create .pngs in static/screenshots for my ./manage collectstatic to get them.

EDIT based on first answer:

I finally created a media directory at the root of my django project

Add the following in settings.py:

MEDIA_ROOT = os.path.join(BASE_DIR, "media")

MEDIA_URL = '/media/'

In urls.py (To make it work with DEBUG=True when in developement state):

from django.conf import settings
## debug stuff to serve static media
if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve',  {'document_root':    settings.MEDIA_ROOT}),
Fumbo
  • 110
  • 8
  • 2
    I think it makes more sense to put your screenshots in `MEDIA_ROOT`. –  Dec 11 '14 at 12:28
  • 1
    I seems that's what I need indeed. Is MEDIA_ROOT usable by default, or do I have to edit mysite settings? What should I include in my template like `{% load staticfiles %}`? – Fumbo Dec 11 '14 at 12:35
  • I found it https://docs.djangoproject.com/en/dev/ref/settings/#media-url I'll confirm if it work in my case – Fumbo Dec 11 '14 at 12:37
  • I think that's pretty much covered in the [documentation](https://docs.djangoproject.com/en/dev/howto/static-files/#serving-files-uploaded-by-a-user-during-development); from what I remember, the deafults are often good enough. –  Dec 11 '14 at 12:37

1 Answers1

2

static files are for your project's assets - css, js, images etc -, IOW things that are part of the project itself and you want to keep in your git/mercurial/whatever scc. Uploaded / dynamic /generated contents are supposed to go to the medias folder (settings.MEDIA_ROOT).

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • As a sub-question : obviously my screen should be readonly, is default `MEDIA_ROOT` safe for that matter? – Fumbo Dec 11 '14 at 12:43
  • What do you mean "read-only" ? the `MEDIA_ROOT` folder is just a folder, just like the `STATIC_ROOT` one, it doesn't make any difference. wrt/ access restrictions, it a system settings thing. – bruno desthuilliers Dec 11 '14 at 12:47
  • Ok my bad. Anyway I put `MEDIA_ROOT = '[django_root]/media/` and `MEDIA_URL = '/media/'` in settings, relaunched the dev serveur but `` is showing no image. The image is in the right folder `[django_root]/media/screenshots/my_image.png` and right name btw. And in my template I have `` – Fumbo Dec 11 '14 at 12:58
  • Directory also have the good right `drwxrwxr-x 3 [me] [me] 4096 déc. 11 13:43 media` I can find what i'm doing wrong – Fumbo Dec 11 '14 at 13:04
  • Based on [this answer](http://stackoverflow.com/questions/2237418/serving-static-media-during-django-development-why-not-media-root) it work adding `url(r'^media/(?P.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT})` to `urls.py` – Fumbo Dec 11 '14 at 13:19