I am beginner on django and trying to create a profile model that interrelate "User" built-in model via One-To-One relation field, it results impossible to span the user model properties by profile model instance and vise versa.
Can someone help me.
models.py
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
users=models.OneToOneField(User, on_delete=models.CASCADE)
image=models.ImageField(default='profilepic.jpg',upload_to='profile_pictures')
location=models.CharField(max_length=100)
def __str__(self):
return self.users.username
profileTemplate.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile</title>
</head>
<body>
<h2>{{user.username}}</h2>
<img src='{{user.Profile.image}}'>
</body>
</html>
CodePudding user response:
use a lowercase profile as per the docs
<img src='{{user.profile.image}}'>