How can I serve media files in a local Django environment?

I can upload the image through the admin page, but the image cannot be found when I go to the URL generated by django. (Error 404) Files are uploaded to the folder:

project_root/media/eventbanner/1/ 

I tried several solutions, but no one works for my situation. Django 1.10 runs locally on Ubuntu 16.04.

I get the URL:

 http://localhost:8000/media/eventbanner/1/banner_image.jpg 

The root folder of the media is located at:

 /home/username/xxx/xxx/project_name/media 

Code in HTML file:

 <div class="banner-image"> <img src="{{ event.eventbanner.banner_image.url }}"/> </div> 

url.py code:

 from django.conf.urls import url, include from django.contrib import admin from . import views from django.conf import settings from django.conf.urls.static import static app_name = 'events' urlpatterns = [ url(r'^$', views.index, name='index'), url(r'^details/(?P<event_id>[0-9]+)/$', views.details, name='details'), url(r'^details/(?P<event_id>[0-9]+)/addcomment/$', views.add_comment, name='add_comment'), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

settings.py

 STATIC_URL = '/static/' STATICFILES_DIRS =[os.path.join(BASE_DIR, 'static'),] MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') MEDIA_URL = '/media/' 

models.py

 def validate_only_one_instance(obj): model = obj.__class__ if (model.objects.count() > 0 and obj.id != model.objects.get().id): raise ValidationError("Can only create 1 %s instance" % model.__name__) class EventBanner(models.Model): event = models.OneToOneField(Event, unique=True) banner_image = models.ImageField(upload_to=get_image_path, blank=True, null=True) def clean(self): validate_only_one_instance(self) 
+7
source share
1 answer

The real problem is that there is no relation between this URL http://localhost:8000/media/eventbanner/1/banner_image.jpg and this location, this is the location on the drive /home/username/xxx/xxx/project_name/media .

In the production application, you will have a web server on which you will store Media content, for the URL will be MEDIA_ROOT and you will MEDIA_ROOT ImageField.url to this value to get a valid image path.

Here you need to configure a web server for your multimedia images. It sounds like a lot of work at first, but Django provides a shortcut ...

File Maintenance in Development

You have some work you need to do to make media files serve locally. This requires some changes in your urls.py ...

 from django.conf import settings from django.views.static import serve # ... the rest of your URLconf goes here ... if settings.DEBUG: urlpatterns += [ url(r'^media/(?P<path>.*)$', serve, { 'document_root': settings.MEDIA_ROOT, }), ] 

It uses the views.serve bit and should only be used in DEBUG mode. It redefines the path to media files (django term for user-uploaded content such as ImageField ). This will redirect these requests through the serve view. Best of all, I can say that it is a mini-web server that will map these request routes to disk locations and allow access to these locations via an HTTP URL.

+16
source

Source: https://habr.com/ru/post/1013744/


All Articles