class UserProfile models Model user models ForeignKey User unique True

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
avatar = models.ImageField(upload_to='avatars/', blank=True)
country = models.CharField(max_length = 30, blank = True, null = True)
website = models.URLField(blank = True, null = True)
class AvatarForm(ModelForm):
class Meta:
model = UserProfile
fields = ['avatar']
def update_avatar(request, name):
if request.method == "POST":
form = AvatarForm(request.POST, request.FILES)
if form.is_valid():
profile_obj = form.save(commit=False)
profile_obj.user = request.user
profile_obj.save()