Hi, I’m new to Django. I wrote a code that has this error. I tried some solutions but they didn’t work and I need help. sorry if it’s a repetitive question but I can’t find the problem.
this is my view.py file:
@login_required(login_url='signin')
def index(request):
user_object = User.objects.get(username=request.user.username)
user_profile = Profile.objects.get(user=user_object)
return render(request, 'index.html', {'user_profile': user_profile})
this is my model.py:
User = get_user_model()
# Create your models here
class Profile(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
id_user = models.IntegerField()
bio = models.TextField(blank=True)
profileimg = models.ImageField(upload_to='profile_images', default='blank-profile-picture.png')
location = models.CharField(max_length=100, blank=True)
def __str__(self):
return self.user.username
class Post(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
user = models.CharField(max_length=100)
image = models.ImageField(upload_to='post_images')
caption = models.TextField()
created_at = models.DateTimeField(default=datetime.now)
no_of_likes = models.IntegerField(default=0)
def __str__(self):
and I get this error:
# DoesNotExist at /
Profile matching query does not exist.
C:\Users\Maedeh\PycharmProjects\social_book\social_book\social_book\core\views.py, line 14, in index
user_profile = Profile.objects.get(user=user_object)