0

Создал личный кабинет вида сайт/account/

Не получается создать личный кабинет вида account/user/

user - пользователь, который зашел

Модель

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=500, blank=True)
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(null=True, blank=True)


    @receiver(post_save, sender=User) 
    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            Profile.objects.create(user=instance)


    @receiver(post_save, sender=User)
    def save_user_profile(sender, instance, **kwargs):
        instance.profile.save()

    def get_absolute_url(self):
        return reverse('profile', args=[self.user])

Вьюха

def profile_user(request, user):

    profile = Profile.objects.filter(user=request.user)
    return render(request, 'account/profile.html', {'profile': 'profile'})

urls

path('<slug:user>/', views.profile_user, name='profile'),

2 ответа 2

2

.filter() возвращает Queryset. А вам нужен сам объект. Используйте .get() вместо .filter()

1

ты во вью пытаешься получить текущего пользователя, а тебе нужно найти пользователя, имя которого совпадает с параметром user + возможно заменить .filter на .get в зависимости от того, как ты хочешь работать в html шаблоне

def profile_user(request, user):
    profile = Profile.objects.get(user__username=user)
    return render(request, 'account/profile.html', {'profile': 'profile'})

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.