Part-12

Adding Update Profile Feature:Django

Update user profile
class UserUpdateForm(forms.ModelForm):
class Meta:
model=User
fields=['username','email']
class ProfileUpdateForm(forms.ModelForm):
class Meta:
model=Profile
fields=['pic','bio']
users/views.py
<form method='POST' enctype="multipart/form-data">
{%csrf_token%}
{{u_form|crispy}}
{{p_form|crispy}}
<button type='submit'>Update</button>
</form>
...
from PIL import Image

class Profile(models.Model):
...
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
img = Image.open(self.pic.path)
if img.mode in ("RGBA", "P"): img = img.convert("RGB")
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.pic.path)

Posting Django Tutorials with Beginner to Advanced Projects