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>
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.
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.
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.
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.
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.
For heroku to serve static files you need to add whitenoise package too. Install it and add the necessary configurations.
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.
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
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")
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With