36π
I have a clue on whatβs the problem. MEDIA_URL
should be like this:
MEDIA_ROOT='<the full path to your media folder>' (i.e: '/home/ike/project/media/')
MEDIA_URL='/media/'
Note the slash character at the beginning. That is because media is a folder in your root server folder and not relative to whatever other url you call it.
And add these lines to the end of your urls.py
file:
# You might need to import static function like this:
#from django.contrib.staticfiles.urls import static
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You can check the following documentation: https://docs.djangoproject.com/en/dev/howto/static-files
Hope this helps
5π
If youβre using the dev server then you need to add something to your urls.py to make django serve the media files, cf:
1.4.x : https://docs.djangoproject.com/en/1.4/howto/static-files/#serving-other-directories
1.5.x: https://docs.djangoproject.com/en/dev/howto/static-files/#serving-files-uploaded-by-a-user
- [Django]-Rendering a value as text instead of field inside a Django Form
- [Django]-Django Footer and header on each page with {% extends }
- [Django]-Django REST Framework (DRF): Set current user id as field value
3π
Check in your settings.py you have define MEDIA_ROOT
and βMEDIA_URLβ (and they are correct). The MEDIA_ROOT specifies an absolute folder on your machine where media will be stored.
So for an example:
MEDIA_ROOT = '/myfolder/'
This would mean it would look for image at:
/myfolder/images/albums/
Next in your settings.py check your MEDIA_ROOT
location: i.e.
MEDIA_URL = 'http://localhost/myfolder/'
So your images:
<img src="{{ MEDIA_URL }}{{ image.image.url }}" height="420"></a>
This would relate to:
http://localhost/myfolder/images/albums/
Hope this helps.
- [Django]-With DEBUG=False, how can I log django exceptions to a log file
- [Django]-Django TypeError: get() got multiple values for keyword argument 'invoice_id'
- [Django]-Using django-admin on windows powershell
2π
Source: https://docs.djangoproject.com/en/dev/howto/static-files/#serving-files-uploaded-by-a-user
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
you need to add to url pattern to serve uploaded files
- [Django]-Django models avoid duplicates
- [Django]-Getting Values of QuerySet in Django
- [Django]-Nginx doesn't serve static
1π
Within your details.html, change your
img src="{{ image.image.url }}" height="420"
To
img src="your_app/media/{{ image.image.url }}" height="420"
I hope this helps. If not I will be glad to provide more details.
- [Django]-What does 'many = True' do in Django Rest FrameWork?
- [Django]-Suddenly when running tests I get "TypeError: 'NoneType' object is not iterable
- [Django]-Django apps aren't loaded yet when using asgi
1π
Well I know this question is old but I solved it right now after prove all options:
- settings.py:
MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
- urls.py DANGER!!! Here one of my errors was that I used my_app/urls.pyβ¦ If you want to use my_app/urls.py you need to add β/namespace_name{{ image.image.url }}β to imgβ src:
from django.conf.urls.static import static from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.conf import settings urlpatterns += staticfiles_urlpatterns() urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
- Your template, detail.html
img src="{{ image.image.url }}" alt="{{ image.title }}"
NOTES:
Yo donβt need MEDIA_URL, be careful with the β/β because image.image.url is absolute, so if you use a namespace then you donβt need to add the finish slash.
img src="/namespace_name/{{ image.image.url }}" --> BAD!!! img src="/namescape_name{{ image.image.url }}" --> GOOD!!!
- [Django]-In django, how do I sort a model on a field and then get the last item?
- [Django]-How to produce a 303 Http Response in Django?
- [Django]-Django models avoid duplicates
0π
I took a little from each of the answers above. I was having the same problem as you. I was getting returns that were being directed from the current /blog/post/(media_url)/image.jpg
In my admin portal I could view it easily and edit it. But on my post.html I was having problems until I added the {{ MEDIA_URL }} β
That is all that I was missing.
I posted my entire section below so that other people could read it and see what they are missing.
post.html:
<label for="id_image"> <img src="{{ MEDIA_URL }}{{ p.image.url }}"
title="{{ p.title }}"> </label>
models.py:
from django.core.files.storage import FileSystemStorage
upload_location =
FileSystemStorage(location='/home/pi/djcode/xpcpro/xpcpro/images')
class Blog(models.Model):
title = models.CharField(max_length=255)
author = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
date = models.DateTimeField(auto_now=False, auto_now_add=True)
body = models.TextField()
image = models.ImageField(
storage=upload_location, null=True,
blank=True, width_field="width_field",
height_field="height_field",)
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
urls.py:
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
settings.py:
MEDIA_ROOT = "/home/pi/djcode/xpcpro/xpcpro/images/"
MEDIA_URL = "/images/"
- [Django]-Django 1.7 β App 'your_app_name' does not have migrations
- [Django]-Django 1.3.1 compilemessages. Error: sh: msgfmt: command not found
- [Django]-Negating a boolean in Django template