V
V
Vitaly Ananiev2022-03-24 17:46:19
Django
Vitaly Ananiev, 2022-03-24 17:46:19

Why doesn't it display the user's photo?

In the root folder of the project there is a folder media,
settings

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

URLs
if not settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

models
class Customer(models.Model):
    user = models.ForeignKey(User, verbose_name='Пользователь', on_delete=models.CASCADE, blank=True, null=True)
    avatar = models.ImageField("Аватар", upload_to="photos/", blank=True, null=True)
    date = models.DateField("Дата создания", auto_now=True, auto_now_add=False, blank=True, null=True)

    def __str__(self):
        return f'{self.user}'

    class Meta:
        verbose_name = "Профиль пользователя"
        verbose_name_plural = "Профиль пользователей"
        ordering = ('id',)

    def profile_absolute_url(self):
        return reverse('profile_detail', kwargs={'pk': self.id})

views
class ProfileDetail(DetailView):
    model = Customer
    context_object_name = 'profile'
    template_name = 'Pay/profile_detail.html'

html
<p><img src="{{ profile.avatar.url }}"></p>
<p>{{ profile.user }}</p>
<p>{{ profile.date }}</p>

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