Access Media Files in Django

I would like to love Django, but this thing of static and media files in development environments turns me on. Please save me from my stupidity.

I am on my development machine. I have a media folder in the root of my project directory.

In settings.py , I have: MEDIA_ROOT = '' and MEDIA_URL = '/media/' .

In urls.py I have:

 if settings.DEBUG: urlpatterns += patterns('', url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, }), ) 

But the only way to get media files is to link to /media/media/ for example. <img src="/media/media/image.png" /> .

I expect (and want)
<img src="/media/image.png" />

Can someone tell me what is happening here and give me a simple recipe for setting up the processing of media files?

Thank you very much.




@ Timmy O'Mahoney - thanks! epic post, and very clear. But he leaves a couple of questions:

(1) I should use /media/ and /static/ , and not media/ and static/ like MEDIA_URL and and STATIC_URL - did I miss something?

(2) If collectstatic hoses /static/ , where you put CSS at the site level, for example. site CSS files? Not in /static/ , obviously.

(3) I put them in the "_" directory from the project root and set STATICFILES_DIRS to point to it - and it looks like the development server gets its static files, despite the urlpatterns directive. If this is not true, where do you add the CSS level of the site during development and what is the workflow around collectstatic when you change them - do you need to edit them in one place and collect them somewhere else after each editing?

+46
django
Jun 20 2018-11-21T00:
source share
5 answers

Why did you set the MEDIA_ROOT parameter empty? This should be the path to your media directory. Since, as you say, your media files are in the media subdirectory, you should put it in MEDIA_ROOT .

+24
Jun 20 2018-11-21T00:
source share

Folder Setting:

Your project root should look something like this:

 /app1 /app2 /media /static /templates urls.py settings.py manage.py 

The media folder should store things like images, downloads, and other materials that can be downloaded during normal use of the website (i.e. after development is complete)

The static folder is supposed to store all CSS / JS and other materials that are part of the development site




Settings.py:

MEDIA_ROOT is the absolute path to the above static directory. This means that it should be something like:

 MEDIA_ROOT = "/User/Bob/Sites/MySite/Project_root/media/" 

MEDIA_URL is the relative URL of the browser with which you must access the media files when you look at the site. It should be (usually)

 MEDIA_URL = "media/" 

which means that all materials can be viewed at http://example.com/media/

Similarly, STATIC_ROOT should be something like

 STATIC_ROOT = "/User/Bob/Sites/MySite/Project_root/static/" 

and STATIC_URL be

 STATIC_URL = "static/" 



File service:

Now that you have told django where these folders should be and the correct URLs to access them, you need to properly handle all folder requests.

Usually, when you are in production, you want the web server to take care of the maintenance of your static files and media files.

If you are developing, you can simply start the django development server to serve them.

To do this, you will tell him to forward the entire request that is included in http://example.com/media to your MEDIA_ROOT and all requests that are included in http://example.com/static to your STATIC_ROOT.

To do this, you add some URLS to URLS.py, as you:

 from django.conf import settings if settings.DEBUG: urlpatterns += patterns('', url(r'^media/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.MEDIA_ROOT, }), url(r'^static/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.STATIC_ROOT, }), ) 



Extra

If you have several applications, each with its own CSS and JS files, you may not want to drop them into one / static / folder. It might be useful to place them in the subfolders of the applications to which they belong:

 /app1/static/ # Specific static folder /app2/static/ /media/ /static/ # Root static folder 

Now your webserver / development server is only looking for static files in which you told it to look (i.e. the root static folder), so you need to collect all the files in subfolders and copy them to the root static folder. You can do it manually, but django provides a command for this for you (this is the whole point of a static application)

 ./manage collectstatic 
+140
Jun 20 '11 at 10:23
source share

I followed the timmy procedure, but got an error that does not have the django.views module django.views . When I use import django.views in my virtualenv everything works fine. This is not a problem with importing a library.

However, I was able to solve this problem by following this procedure in my main urls file

 from django.conf.urls.static import static urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

https://docs.djangoproject.com/en/dev/howto/static-files/

+12
Sep 10 '13 at 19:00
source share

In settings.py make sure you add

django.core.context_processors.media

in your TEMPLATE_CONTEXT_PROCESSORS. Otherwise, MEDIA_ROOT will not work when you use it in templates.

+3
Jul 22 '15 at 23:14
source share

I am using Django 1.10. And my media folder is "uploads" This is the setting in my .py settings:

 MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads') MEDIA_URL = '/uploads/' 

And in the template, I put the name o my MEDIA_URL in front of the object object.name instead of object.url as follows:

  <img src="uploads/{{ imagen_identificativa.name }} " alt="{{imagen_identificativa}}"> 

And it works for me. Hope this helps.

0
Mar 29 '17 at 4:42 on
source share



All Articles