119π
Add media url entry in your project urlpatterns:
from django.conf.urls.static import static
from django.conf import settings
...
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
35π
The better way for MEDIA_ROOT is,
try to make media path dynamic will be easy when you shift your project.
Settings.py
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')
MEDIA_URL = '/media/'
urls.py
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)
Look at this
- [Django]-Django TemplateDoesNotExist?
- [Django]-How to use "AND" in a Django filter?
- [Django]-How to redirect with post data (Django)
6π
Just to add: in case the other answers do not work for you, try putting the static url before the other ones. Like so:
urlpatterns = static(...) + [...]
What may be happening is that some of your patterns in the list prevent the request from reaching the static handlers. So putting the static handlers first solves this. Worked for me.
- [Django]-How to find out the request.session sessionid and use it as a variable in Django?
- [Django]-How to squash recent Django migrations?
- [Django]-Collectstatic error while deploying Django app to Heroku
4π
This is a server error. Iβm assuming you are using Nginx. Just add this in your Nginx Configuration file(/etc/nginx/sites-available/example.com) just under location /static/
location /media/ {
root /home/user/myprojectdir;
}
Here, user should be your username you created and myprojectdir should be your project directory.
- [Django]-110: Connection timed out (Nginx/Gunicorn)
- [Django]-How do I restrict foreign keys choices to related objects only in django
- [Django]-Mixin common fields between serializers in Django Rest Framework
1π
In template create link by anchor tag and add .url in the end to that fileobject
e.g
{% for post in post %}
<a href="{{post.imagefilename.url}}" >
{% endfor %}
- [Django]-Django: Converting an entire set of a Model's objects into a single dictionary
- [Django]-Troubleshooting Site Slowness on a Nginx + Gunicorn + Django Stack
- [Django]-Django Rest Framework: Dynamically return subset of fields
0π
In my development server I fixed it by commenting out these lines in settings.py
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
- [Django]-Filtering dropdown values in django admin
- [Django]-Generating a Random Hex Color in Python
- [Django]-Django REST framework serializer without a model