Django 2.2 Media files

37 views
Skip to first unread message

Michał Ratajczak

unread,
Jul 5, 2019, 8:56:01 AM7/5/19
to Django users
Hi, i'm new in Django, I'm trying to configure media files correctly.

That's in my model.py:

class Question(models.Model):
    description = models.CharField(max_length=200)
    image = models.ImageField(upload_to='media/', null=True, blank=True)


in settings.py:

STATIC_URL = '/static/'
MEDIA_URL = '/media/'

ENV_PATH = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(ENV_PATH, '../public/static/')
MEDIA_ROOT = os.path.join(ENV_PATH, '../public/media/')

in urls.py:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('barber.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

in view.py:
def index(request):
    images = Question.objects.values('image')
    template = loader.get_template('barber/index.html')
    context = {
            'images': images
        }
    return HttpResponse(template.render(context, request))

I'am adding an image via admin site and next i render in template. Url for image is "media/image.png" bit file is in .../media/media/image.png, so i can't understand it. Can anyone help me ?

John Bagiliko

unread,
Jul 5, 2019, 10:22:22 AM7/5/19
to django...@googlegroups.com
Remove the upload_to in the model.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5613b376-575f-4ab0-bdff-368f0fe8f1cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Bagiliko

unread,
Jul 5, 2019, 10:25:34 AM7/5/19
to django...@googlegroups.com
Since you already configured static folder called media in settings.py, if you specifify upload_to='/media', Django will create a folder 'media' inside the media folder.

Anonymous Anon

unread,
Jul 5, 2019, 1:42:16 PM7/5/19
to django...@googlegroups.com
I just find it really useful to save it in Google cloud

Michał Ratajczak

unread,
Jul 5, 2019, 3:38:04 PM7/5/19
to Django users
I Remove upload_to and it still wasn't working and next i did in my template.html:

{% get_media_prefix as STATIC_PREFIX %} <- i added this line because {{ item.image }} returns only 'image.jpg'. Now a url is correct :)
{% for item in images %}
                    <img class="container-images-image" src={{ STATIC_PREFIX }}{{ item.image }}/>
                {% endfor %}
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Jani Tiainen

unread,
Jul 5, 2019, 4:14:52 PM7/5/19
to django...@googlegroups.com
Hi.

What you did is correct and expected.

Upload_to is relative path to MEDIA_ROOT in case of normal file upload backend. There are many others like S3.

Now MEDIA_URL is absolute path of web server which points to MEDIA_ROOT. In development you can use static file serving trick like you did.

Now when getting url in template you should use something like {{ image.url }} which should render correct absolute url to your  uploaded file.

HTH.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Michał Ratajczak

unread,
Jul 5, 2019, 4:45:32 PM7/5/19
to Django users
Oh yeah! That's it ! Big thank's for you :)
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

John Bagiliko

unread,
Jul 6, 2019, 5:53:11 AM7/6/19
to django...@googlegroups.com
You need {{ item.image.url }} on your template since you are iterating through the context you passed in views. I'm sure you did something like this in views 
def home(request)
    images = Your_model.objects.all()
    context = {'images': images}
    return render(request, 'path_to/your/template', context)

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.


--
Regards
JOHN BAGILIKO
MSc. Mathematical Sciences (Big Data and Computer Security)
African Institute for Mathematical Sciences (AIMS) | AIMS Senegal
Reply all
Reply to author
Forward
0 new messages