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.

pe 5. heinäk. 2019 klo 15.55 Michał Ratajczak <luminousshado...@gmail.com>
kirjoitti:

> 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 ?
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@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
> <https://groups.google.com/d/msgid/django-users/5613b376-575f-4ab0-bdff-368f0fe8f1cf%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CAHn91odFeKH_FkZAuJ73w4scBbicSYsV%2BSpCD74P07Y%2B-%3DjC6A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to