K
K
Kest-iv2017-04-04 23:26:18
Django
Kest-iv, 2017-04-04 23:26:18

Django 1.10. How to connect pictures from media?

Good afternoon. I just can't get djanga to display user avatars that are in MEDIA_ROOT. I found a similar question, but the answer to it did not help me.
models.py

class User(AbstractBaseUser, PermissionsMixin):
    ...
    avatar = models.ImageField(upload_to=get_avatar_path, blank=True, null=True)
    ...

critical settings in settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

views.py
class UserDetailView(DetailView):
    model = User
    context_object_name = 'user_obj'

    def get_context_data(self, **kwargs):
        context = super(UserDetailView, self).get_context_data()
        context.update({'data': self.get_info()})
        return context

root urls.py
from django.conf.urls import url, include
from django.conf.urls.static import static
from django.contrib import admin
from django.conf import settings
from django.views.generic import RedirectView
from django.views.static import serve
from django.contrib.staticfiles import views


from posts import views as post_views
from authentication import views as auth_views

urlpatterns = [
    url(r'^admin/', admin.site.urls, name='admin'),
    url(r'^posts/', include('posts.urls', namespace='posts')),
    url(r'^$', RedirectView.as_view(url='posts/', permanent=True)),
    url(r'accounts/', include('django.contrib.auth.urls')),
    url(r'^auth/', include('authentication.urls', namespace='authentication')),
    url(r'^users/', include('core.urls', namespace='core')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


# urlpatterns += staticfiles_urlpatterns()

#
# if settings.DEBUG:
#     urlpatterns += [
#         url(r'^media/(?P<path>.*)$', serve, {
#             'document_root': settings.MEDIA_ROOT,
#         }),
#     ]

I also tried the commented options, but to no avail.
Piece of template
{% extends 'base.html' %}
{% block title %} User: {{ user_obj.username }} {% endblock title %}
{% block content %}
{% load static %}
<div class="container">
    <div class="content">
        <div class="personal-data">
            <div class="row user-head">
                <div class="left-col">
                    <img src="{{ user_obj.avatar.url }}" width="96px" height="96px">
                </div>

The error looks like this
Page not found (404)
Request Method: GET
Request URL: 127.0.0.1:8000/media/home/username/djcode/blog/blo...
Raised by: django.views.static.serve
How to be? :)
UPDATE:
As devalone rightly noted, the path in upload_to to the file should not be absolute.
Fixed it and everything worked.
Thanks to all

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question