not able to access uploaded media files in django - django

This is the error i am getting
template does not exist
This is my settings file I have added media root and media directory in settings.py
These are my urls urls

add this to your urls.py file
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Related

Django medias(pictures) suddenly not loading

I am developing a website with Django, I had a lot of pictures in my project, uploaded from the admin panel and saved in the Media folder which I created for these uploads separately, It was working fine and exact way I wanted in months, Suddenly they are just not loading, getting 404 for all of them, without any change in project, they are just not loading.
My media path in Settings.py :
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
I have added this to the end of my urls.py of the app:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and as i said, it was working fine for a long, suddenly this happened
edit: I just figured it out that this is happening when I am using redirect function in one of my views
Another way to do it is:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# other URLs ...
]
# If DEBUG=True in the settings file
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
So we're adding static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) to the urlpatterns if DEBUG=True in the settings.py file. This gives a better management in some sense.
I had the same issue and this helps me :
Add this in your urls.py in the urlpatterns list :
from django.views.static import serve
from django.conf import settings
from django.conf.urls import url
urlpatterns = [
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
# YOUR URLS ARE HERE
]
Of course stay in Debug=True in settings.py.

Image url not displaying my uploaded images when django project loaded onto heroku site

My uploaded images are not loading when uploaded to heroku and I think its not related to static file issue. When i set debug = False they come fine and i know that when it is false django uses itself to host the static assests. So setting that and my images loads fine but that's not the case if i set Debug = True on heroku or on my local env. Can somebody help me solve this issue.
template
<img class="img-fluid" style="height:auto;" src="{{post.work_img.url}}" alt="Generic placeholder image">
model
class WorkExp(models.Model):
work_img = models.ImageField(upload_to="work_images/")
app/urls.py
urlpatterns = [
path("carrer/", WorkExpView, name="carrer_page"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
project/urls.py
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("blog.urls", namespace="blog")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In project/urls.py try this
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
instead of
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and set the debug = True
Check if you have static_root and are using python manage.py collectstatic.

Storing images in Django model and media folder

I have been following this post
However I do not understand this part in which the answer states
In url.py
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [....
]
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Now
In my project if I add
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
after my
urlpatterns = [....
]
I get the error
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
TypeError: bad operand type for unary +: 'list'
any suggestions on how i can fix this ?
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Django not serving media files if I check for settings.DEBUG

This serves the media files correctly:
urlpatterns = [
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This doesn't serve the media files:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [ ... ]
if settings.DEBUG:
urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The documentation writes "This is not suitable for production use!", so that is why I need a way to check for DEBUG before serving media files. How can I do that. Why does this approach doesn't work?
Use
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
(notice '=' after '+' - in your version you are adding static() patterns but not assigning the result to the urlpatterns variable)

Unable to view media files in django

I have correctly set the settings for media in settings.py as:-
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and also specified the correct url settings
if settings.DEBUG:
urlpatterns += patterns(
'django.views.static',
(r'^media/(?P<path>.*)',
'serve',
{'document_root': settings.MEDIA_ROOT}), )
Whenever i upload an image through admin..the image gets uploaded in my media folder but i can't see it when i run my development server...it shows a page not found (404) error...
I think the problem is due to some permissions of uploaded files which is why django shows a 404 page when trying to access them..if someone knows about this please help
Please suggest me a solution
Thank you!
Try this:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Relevant url:
https://docs.djangoproject.com/en/1.7/howto/static-files/#serving-files-uploaded-by-a-user-during-development
Do this
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = patterns('',
# your urls here.
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)