J
J
Juvel19882021-03-02 23:28:10
Django
Juvel1988, 2021-03-02 23:28:10

Why is the user's avatar not displaying correctly?

I'm writing a news site in Django and ran into the following problem. I extended the Django User Model with the AbstractUser method, adding a biography and an avatar, using the User model.
At the moment I have two users created in the admin panel. Both are authors of articles. The problem is that when publishing news from the second author, he displays the same avatar as the first one, although they are different. Why is this happening?

models.py

class User(AbstractUser):
    bio = models.TextField(max_length=500, blank=True, verbose_name='Информация о пользователе: ')
    avatar = models.ImageField(verbose_name='Аватар: ')

class News(models.Model):#Новости
    title = models.CharField('Заголовок', max_length=250)
    slug = models.SlugField(max_length=50,unique=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, 
    related_name='news_posts', null=True, blank=True)
    main_photo = models.ImageField('Постер', null = True, blank = True, upload_to='prosvet/media/images/main')
    anons = models.CharField(max_length=350)
    body = RichTextUploadingField(
                                    null=False,
                                    blank=False,
                                    # config_name='toolbar_Custom',
                                    external_plugin_resources=[(
                                        'youtube',
                                        '/static/ckeditor/ckeditor/plugins/youtube/',
                                        'plugin.js',
                                    )],
                                    )
    date_added = models.DateTimeField(auto_now_add=True)


admin.py
from django.contrib import admin
from prosvet_logs.models import About_us, News, Contacts, Cooperation, Dialogs, Monologs, Projects, Mission, User
admin.site.register(User)
class PostAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("title",)}


settings.py vews.py

AUTH_USER_MODEL = 'prosvet_logs.User'


def articles(request):#Страница новостей
        news = News.objects.order_by('-date_added') 
        context = {'new': news}
        return render(request, 'prosvet_logs/news.html', context)


def el(request, el_id): #Вывод отдельной новости
    news = News.objects.get(id=el_id)
    context = {'news':news}
    return render(request, 'prosvet_logs/article.html', context)


article.html - news is rendered here
{% extends "prosvet_logs/base.html" %}
{% load static %}
{% block content %}

<link rel="stylesheet" href="{% static 'styles/main.css' %}">
<div>
    
    <h2>{{ news.title }}</h2>
    <p>{{ news.date_added|date:'M d, Y H:i' }}</p>
    <p>{{ news.body|safe }}</p>

    <div id="author">
        <p> {{ news.author }}</p>
        <p><img src="{{ user.avatar.url }}" height="160" align="bottom" /></p>
    </div>

</div>

{% endblock content %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Ryabykin, 2021-03-03
@Juvel1988

view just the same and plays a key role) but not in this case
, use

<p><img src="{{ news.author.avatar.url }}" height="160" align="bottom" /></p>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question