Answer the question
In order to leave comments, you need to log in
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)
...
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
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
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,
# }),
# ]
{% 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>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question