36π
β
Your image would be uploaded to the media
folder, so itβs better to change the path in the model like images/
, and they will be uploaded to media/images
In settings.py
add this
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
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)
And then, if you want to display all these images, use something like this
in view.py
BlogContent.objects.all()
And render it like this:
{% for img in your_object %}
<img src="{{ img.image.url }}" >
{% endfor %}
π€Ivan Semochkin
17π
static
in upload_to
doesnot make sense, since user-uploaded images go into media/
folder.. you need these:
image = models.ImageField(upload_to='blog/%Y/%m/%d')
and all images land in:
media/blog/2016/01/02/img_name.jpg
you access it in template like this:
<img src="{{ blog.image.url }}">
in settings:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
π€doniyor
- [Django]-Multi-tenant Django applications: altering database connection per request?
- [Django]-How do I use CommaSeparatedIntegerField in django?
- [Django]-How do I package a python application to make it pip-installable?
- [Django]-RetrieveAPIView without lookup field?
- [Django]-Apache or Nginx to serve Django applications?
- [Django]-Django β how to detect test environment (check / determine if tests are being run)
Source:stackexchange.com