Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images(Media) not displaying on django-heroku server

I had uploaded my site Django app on Heroku server when I upload image file is successfully uploaded and image path as per settings also fetch properly but the image is not displaying it give error media file not found in a server this is settings media setting

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

this is in url.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('UserView.urls')),
    path('caterer/',include('CaterView.urls')),
]
# if settings.MediaAllow:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

this is models.py

class TypeofFood(models.Model):
    tyf_id = models.AutoField(primary_key=True,auto_created=True)
    tyf_value = models.CharField(max_length=256)
    tyf_image = models.ImageField(upload_to="typeoffood/", null=True, blank=True,default='default.jfif')

in template it fatch image like this

<center><img src="{{i.tyf_image.url}}" class="img-responsive" style="height: 200px; width: 200px; border-radius:50%" alt="Image of Caterers"></center>
like image 315
Irtaza Hussain Avatar asked Nov 30 '19 09:11

Irtaza Hussain


People also ask

How do I view media files in Django?

The problem is that Django development server doesn't serve media files by default. To make Django development server serve static we have to add a URL pattern in sitewide urls.py file. Now visit http://127.0.0.1:8000/media/python.png again, this time you should be able to see the image.

How do I add an image to Django?

In django we can deal with the images with the help of model field which is ImageField . In this article, we have created the app image_app in a sample project named image_upload. The very first step is to add below code in the settings.py file. MEDIA_ROOT is for server path to store files in the computer.

Can I use Django with Heroku?

Once you have your Django project ready, you're going to sign up for a free Heroku account. Next, you'll download a convenient command-line tool that will help you manage your apps online. As demonstrated in the screencast above, the command line is a quick way of working with Heroku.

Why can't I store my images on my Heroku server?

We are doing this because we are not able to store our images Media on our heroku server specifically. and connect it to your heroku app like I did here with " computervisiontools " a Django app in my heroku dashboard Also choose a plan to connect with I am choosing Free version here.

What is a media file in Django?

Media files in Django are a particular variant of static files. Media files are read from the server's file system as well. Unlike static files, though, they are usually generated files uploaded by users or generated by your application and are associated with a model's FileField or ImageField.

How to serve static files on Heroku?

For heroku to serve static files you need to add whitenoise package too. Install it and add the necessary configurations.

Why do we need to add Whitenoise to Heroku?

because it is like testing purpose, if you want to upload and store media file on heroku you can use third party like whitenoise go to the link and learn how to use whitenoise to upload media file on heroku, you can check this link also. Show activity on this post. For heroku to serve static files you need to add whitenoise package too.


4 Answers

heroku free storage is not allow media file store, that's why your media file will be deleted after upload

because it is like testing purpose, if you want to upload and store media file on heroku you can use third party like whitenoise

go to the link and learn how to use whitenoise to upload media file on heroku, you can check this link also.

happy codding

like image 105
Nj Nafir Avatar answered Oct 23 '22 06:10

Nj Nafir


For heroku to serve static files you need to add whitenoise package too. Install it and add the necessary configurations.

MIDDLEWARE_CLASSES = (
    # Simplified static file serving.
    # https://warehouse.python.org/project/whitenoise/
    'whitenoise.middleware.WhiteNoiseMiddleware',
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

STATIC_ROOT = os.path.join(BASE_DIR, "your_static_folder")

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

MEDIA_URL = "/media/"

MEDIA_ROOT = os.path.join(BASE_DIR, "your_media_folder")
like image 38
bmons Avatar answered Oct 23 '22 06:10

bmons


In your html, you will have to serve your image like so:

{% load static %}
<img src="{% static "images/hi.jpg" %}" alt="Hi!">  

The "image.url" format in the html does not work on staticfiles/Heroku
I ran into a similar issue on Heroku, and solved my issue using these links below.

Here is a complete guide for serving images on Heroku: 

https://github.com/codingforentrepreneurs/Guides/blob/master/all/Heroku_Django_Deployment_Guide.md

I would recommend going through the references also. These would be the references:

1) http://whitenoise.evans.io/en/stable/django.html

2) https://docs.djangoproject.com/en/3.0/howto/static-files/

3) https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#std:templatetag-static
like image 28
ilayas88 Avatar answered Oct 23 '22 06:10

ilayas88


1-Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.

2-In your settings file, define STATIC_URL, for example:

STATIC_URL = '/static/'

3-In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE:

{% load static %}
<img src="{% static 'my_app/example.jpg' %}" alt="My image">

4-Store your static files in a folder called static in your app. For example: my_app/static/my_app/example.jpg.

like image 35
Negar37 Avatar answered Oct 23 '22 06:10

Negar37