Объект Django 'NoneType' не имеет атрибута 'socialaccount_set'

models.py

class Post(models.Model):
    title = models.CharField(max_length=30)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)

    def get_avatar_url(self):
        if self.author.socialaccount_set.exists():
            return self.author.socialaccount_set.first().get_avatar_url()
        else:
            return 'https://dummyimage.com/50x50/ced4da/6c757d.jpg'

часть файла landing.html

    <div class="col">
        <h2>Recent Posts</h2>
            {% for post in recent_posts %}
                    <div class="card">
                        <div class="card-body">
                            <h6>
                                <a href="{{post.get_absolute_url}}" 
                                    class="text-decoration-none text-dark">
                                    {{post.title}}
                                </a>
                            </h6>
                            <span class="badge">
                                <img class="rounded-circle" width="20px" src="{{post.get_avatar_url}}">
                                {{post.author.username}} {{post.created_at}}
                            </span>
                        </div>
                    </div>
            {% endfor %}
    </div>

Сообщение об ошибке

'NoneType' object has no attribute 'socialaccount_set'

Error during template rendering

In template /.../landing.html, error at line 34(which includes '{{post.get_avatar_url}}' source url.)

Когда я вошел в систему, все работает нормально, но когда я не вошел в систему, он показывает ошибку вместо того, чтобы показать манекен. Вызывает ли это получение socialaccount_set из allauth lib? Html и Post модель находятся в разных приложениях.

views.py для приложения secondApp!

from firstApp.models import Post

def landing(request):
    recent_posts = Post.objects.order_by('-pk')[:3]
    return render(request, 'secondApp/landing.html', {
        'recent_posts': recent_posts,
    })
Вернуться на верх