При публикации поста от других аккаунтов в авторе высвечивается мой суперпользователь
models.py (Мой кастомный пользователь)
from django.db import models
from django.contrib.auth.models import User
from PIL import Image
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
avatar = models.ImageField(default='default.png', upload_to='profile_pics')
bio = models.CharField(max_length=150, blank=True)
def __str__(self):
return f'{self.user.username} Profile'
def save(self, *args, **kwargs):
super().save()
img = Image.open(self.avatar.path)
if img.height > 300 or img.width > 300:
new_img = (300, 300)
img.thumbnail(new_img)
img.save(self.avatar.path)
На всякий случай, settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'utopies',
'main',
'itutopies',
'regauth',
'crispy_forms',
'taggit',
]
Ну и наконец, мое приложение со статьями models.py (utopies)
from django.db import models
from django.conf import settings
from taggit.managers import TaggableManager
User = settings.AUTH_USER_MODEL
class Utopies(models.Model):
author = models.ForeignKey(User, on_delete = models.CASCADE, default='1')
title = models.CharField('Название', max_length=30)
anons = models.CharField('Анонс', max_length=25)
tags = TaggableManager()
full_text = models.TextField('Утопия')
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return f'/utopies/{self.id}'
class Meta:
verbose_name = 'Утопия'
verbose_name_plural = 'Утопии'
views.py
@login_required
def utopiaform(request):
error = ''
if request.method == 'POST':
form = UtopiesForm(request.POST)
if form.is_valid:
obj = form.save(commit = False)
obj.user = request.user;
obj.save()
return redirect('utopies')
else:
return redirect('home')
form = UtopiesForm()
context = {
'form': form
}
return render(request, 'utopies/utopiaform.html', {'form': form})
def utopies(request):
news = Utopies.objects.order_by('-date')
return render(request, 'utopies/utopies.html', {'news': news})
signals.py
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .models import Profile
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
profile = Profile.objects.get(user = request.user)
instance.profile.save()
@receiver(post_save,sender=User)
def update_user_profile(sender,instance,created,**kwargs):
if created:
profile = Profile.objects.create(user =instance)
apps.py
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'regauth'
def ready(self):
import regauth.signals
class RegauthConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'regauth'
utopies.html
{% for el in news %}
<a href="{% url 'utopia' el.id %}">
<div class="blur10px filter u-container-style u-group u-hover-feature u-opacity u-opacity-10 u-radius-30 u-shape-round u-group-3">
<div class="u-container-layout u-container-layout-3">
<h3 class="u-custom-font u-font-montserrat u-text u-text-body-alt-color u-text-6">{{el.date}}</h3>
<h3 class="u-custom-font u-font-montserrat u-text u-text-body-alt-color u-text-7">{{el.author.last_name}}<br>{{el.author.first_name}}
</h3>
<h3 class="u-custom-font u-font-montserrat u-text u-text-body-alt-color u-text-8">@{{el.author}}</h3>
<h3 class="u-custom-font u-font-montserrat u-text u-text-body-alt-color u-text-9">{{ el.tags.all|join:", " }}<span style="text-decoration: underline !important;"></span>
</h3>
<div class="u-hover-feature u-image u-image-circle u-preserve-proportions u-image-1" alt="" src="{{author.profile.avatar.url}}"data-image-width="900" data-image-height="900"></div>
<h3 class="u-custom-font u-font-montserrat u-text u-text-body-alt-color u-text-10">{{el.title}}</h3>
<h3 class="u-custom-font u-font-montserrat u-text u-text-body-alt-color u-text-11">{{el.full_text}}</h3>
</div>
</div>
</a>
<br>
{% endfor %}
По какой причине высвечивается мой супер пользователь, вместо автора поста. У меня получалось исправлять эту проблему, но когда я выхожу из аккаунта (автора поста) и хочу через другой аккаунт что либо запостить, высвечивается мой супер пользователь. Если нужен код регистрации пользователя, пишите.