Images from media folder is not displaying django template - django

I am having images in my media folder and I want to display them but Django is not displaying images other than static folder.
In my setting
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static'),]
MEDIA_ROOT = BASE_DIR
MEDIA_URL = '/media/'
In my urls.py after url pattern I added this
+ static(MEDIA_URL, document_root=MEDIA_ROOT)
In my templates
<img class="card-img-top" style="" src="{{result.object.thumbnail.url}}" alt=""></a>
<p>{{result.object.thumbnail.url}}</p>
It is showing the correct path but not showing the image, I am unable to figure out the problem. Thanks

Your MEDIA_ROOT have the path of the root of your project. You must join to it, the direcotry of your media. (I suppose media/ is the directory name where you upload all your media)
I think you should have your MEDIA_ROOT like that.
MEDIA_ROOT = os.path.join(BASE_DIR,'media')

MEDIA_ROOT have root path of your project. MEDIA_URL must be correct. Check following
setting.py of your project
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py of project
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url('', include('app1.urls')),
url('home/', myapp_views.index)
] + static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
I was facing same error from the last 5 days , follow django documentation, many tutorials but nothing work for me but finally I found the solution
I am showing the index.html on home means I have not given any endpoint in url of my app and project url file
app/urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url('', 'views.index', name='index page'),
]
project/urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url('', include('app1.urls')),
url('', myapp_views.index)
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Rest of code remains the same and I changed the following in both files
app/urls.py
url('home/', 'views.index', name='index page'),
project/urls.py
url('home/', myapp_views.index)
That's work for me. Accept the answer if also work for you

This is a chunk of my HTTP logs from django v3.2 running wagtail. Looks as though django is trying to tell me where the missing media is but not able to. Because this is a thicket page with gallery sub-images of "featured pages" the view source in my browser doesn't reveal the image file attempt, but I am assuming it is the same issue as OP's with misconfigured MEDIA_ROOT. Strangely, not seeing any errors in the page's linked images when I bring the child page up in wagtail admin. Anyone have an idea why the missing image won't bubble up to the HTTP logs, or what causes the "media/not-found" substitution for the "real" item causing the 404?
[25/Aug/2021 20:28:53] "GET /static/wagtailadmin/images/bg-dark-diag.svg HTTP/1.0" 200 700
Not Found: /media/not-found
[25/Aug/2021 20:29:18] "GET /media/not-found HTTP/1.0" 404 3252
Not Found: /media/not-found
[25/Aug/2021 20:29:18] "GET /media/not-found HTTP/1.0" 404 3252

Related

How to fix Django Summernote using relative path for src url in image integration?

I am new to the Django framework, and inherited a projet which I am trying to fix.
Right now my project runs in a docker at url localhost:8000 until deployment in on a remote server.
I have the django-summernote extension installed for fancy text editing and image integration, but it doesn't work as expected:
Summernote Editor in Django admin works fine, and correctly displays uploaded images
But on my content pages, the html integration of my provides an incorrect src url (which return an error ofc)
src="/media/django-summernote/2022-02-27/90a1f1ec-ed16-4cc2-a9f8-b0bb30ea4ab8.png"
The expected html integration should be :
src="http://localhost:8000/media/django-summernote/2022-02-27/90a1f1ec-ed16-4cc2-a9f8-b0bb30ea4ab8.png"
I checked and the image file does exist in the folder, the expected url below does actually shows the picture alright as well.
The rest of the website uses images integration just fine when not related to summernote.
I figure this has something to do with settings.py or even router (which tbh is still a bit complex in my head).
settings.py
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
urls.py
urlpatterns = [
...
path("summernote/", include("django_summernote.urls")),
]
Edit: running Django 3.1
For Django >= 1.7
Just add this into your
urls.py
urlpatterns = [
...
path("summernote/", include("django_summernote.urls")),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You no longer need if settings.DEBUG as Django will handle ensuring this is only used in Debug mode.
For Django <= 1.6
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
Try this:
settings.py
MEDIA_ROOT = BASE_DIR / "media"
MEDIA_URL = "/media/"
urls.py
urlpatterns = [
...
path("summernote/", include("django_summernote.urls")),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Django Template Image not displaying even with media url and media root

Static images are showing properly. The files in the media folder are not displaying in html. I tried setting up media_url in various ways but in vain. I uploaded the image via django admin panel. The name of product is showing fine. The img.url shows /media/p2.jpg
models.py
class Product(models.Model):
name = CharField(("Name"),max_length=256,blank=False)
title_img = models.ImageField(null=True, blank=True)
settings.py
PROJECT_ROOT = (os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(PROJECT_ROOT), "media_root")
urls.py
urlpatterns = [
path('', ProductList.as_view() , name="product"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
template.html
{{p.name}}
<img src="{{p.title_img.url}}">
the name is displayed while image is not displayed. In console, I get
"GET /media/p2.jpg HTTP/1.1" 404 2690
I changed some settings and was able to resolve it. Probably the mistake was that I was changing project_root/products/urls.py instead or project_root/urls.py. Overall changes I did:
In main urls.py, added these
urlpatterns = [
path('', ProductList.as_view() , name="product"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Changed settings.py
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/')
MEDIA_URL = '/media/'
As per the documentation: https://docs.djangoproject.com/en/3.1/howto/static-files/#serving-static-files-during-development and https://docs.djangoproject.com/en/3.1/howto/static-files/#serving-files-uploaded-by-a-user-during-development
it should be used only for development purposes,
settings.DEBUG must be set to True,
it works only if the url is local
in your case, 1 and 3 seams ok but is your DEBUG setting set to True?
As per the documentation: https://docs.djangoproject.com/fr/3.1/ref/contrib/staticfiles/#runserver, you can runserver with argument --insecure so you can still serve static files or media file with DEBUG set to False.

Django MEDIA_URL appending full MEDIA_ROOT

In Django 3 I have this in my settings: MEDIA_URL = '/media/'
My MEDIA_ROOT echos out properly (/home/bradrice/source/repos/bb_backend/media
) and I have this in my urls:
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I am using a VersatileImageField and the image uploads to the proper media folder. However, I can't see the image in Admin and if I click the link it prepends the Url with the full MEDIA_ROOT instead of just the /media/ onto the display url.
What am I doing wrong.
My error. In my models I had this code to make the upload path:
def artwork_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT / user_<id>/<filename>
return MEDIA_ROOT + '/artwork/{0}'.format(filename)
then in my image model I had upload_to=artwork_directory_path.
I just hardcoded it to upload_to="artwork" and everything started working.

Can someone explain me this line? I got this from Telesko Django playlist video number 20

This urls.py of base project
urlpatterns = urlpatterns + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
This is the settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
STATIC_ROOT=os.path.join(BASE_DIR,'assets')
MEDIA_URL ='/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
This code is to add items to the database dynamically and i am not able to understand why is he adding the urlpatterns.
He is adding media urls to the url patterns. So for example if you had an image or video or something stored in your django project, you can use the browser to access these files at MEDIA_URL. In the settings.py, you are setting MEDIA_URL (where you can go in the browser '/media/'), to point to the contents of your MEDIA_ROOT (the 'media' folder)

Serving media files in Django

I am running Django in development. In settings.py I have set up the MEDIA_URL
MEDIA_ROOT = os.path.join(BASE_DIR, 'fileuploader/uploaded_files')
MEDIA_URL = 'fileuploader/uploaded_files/'
Then in ursl.py I have,
if settings.DEBUG:
urlpatterns += patterns('', url(r'^media/(?P<path>)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT,}),)
As far as I understand this should mean that any url media/filename will serve the file instead just requesting it.
In the template, through the model i am able to get to the file name and url. But i can't make this into a linkable path to download the file.
<p>File URL link media{{ item.upload}}</p>
Incidently item.upload and item.upload.name produce the same string.
The file name in the filestore is ./TESTFILE.txt Do I need to strip the './' at the beginning?
Commit 26 is the project https://github.com/shanegibney/djangoForum
Thanks
I added the MEDIA_URL tag and this allows files to download.
<p>URL media{{ item.upload}}</p>
Also in urls.py to the end I added,
urlpatterns = [
......
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)