How to save bio using forms.ModelForm

14 views
Skip to first unread message

Arturo Fernandez

unread,
Jan 27, 2019, 12:22:25 PM1/27/19
to Django users

I tried multiple links on stackov. but none worked, I can't get my bio form to save the data on the db for the user profile :/

I managed to save the first name and last name, but I can't make the bio save... This is my code:


profile.html



<form method="POST">
{% csrf_token %}
{{ user_BioAndSocialForm.bio |as_crispy_field }}
</p>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Update</button>
</div>
</form>


views.py



from django.shortcuts import render, redirect
from django.contrib import messages # to display alert messages when the form data is valid
from .forms import UserSignUpForm, UserUpdateForm, ProfileUpdateForm, UserProfileForm, BioAndSocialForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth import update_session_auth_hash
from django.http import HttpResponseRedirect
from django.shortcuts import HttpResponse
from django.contrib.auth.forms import PasswordChangeForm

@login_required
def profile(request):
if request.method == 'POST':
user_ProfileForm = UserProfileForm(request.POST, instance=request.user)
user_BioAndSocialForm = BioAndSocialForm(request.POST, instance=request.user)

if user_ProfileForm.is_valid() and user_BioAndSocialForm.is_valid():
user_ProfileForm.save()
user_BioAndSocialForm.save()
messages.success(request, f'Profile updated!')
return HttpResponseRedirect(request.path_info)
else:
messages.error(request, _('Please correct the error below.'))

else:
user_ProfileForm = UserProfileForm(instance=request.user)
user_BioAndSocialForm = BioAndSocialForm(instance=request.user)

context = {
'user_ProfileForm': user_ProfileForm,
'user_BioAndSocialForm': user_BioAndSocialForm
}

return render(request, 'users/profile.html', context)


forms.py


from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Profile
from crispy_forms.helper import FormHelper

class BioAndSocialForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['bio']

def __init__(self, *args, **kwargs):
super(BioAndSocialForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_show_labels = False


models.py


from django.db import models
from django.contrib.auth.models import User
from PIL import Image

# Profle model with data regarding the user
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500, blank=True)
birth_date = models.DateField(null=True, blank=True)

#Image feature upload
image = models.ImageField(default='default.jpg', upload_to='profile_pics')

# If we don't have this, it's going to say profile object only
def __str__(self):
return f'{self.user.username} Profile' # it's going to print username Profile

def save(self, *args, **kwargs):
super().save(*args, **kwargs)

img = Image.open(self.image.path)

if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.image.path)



This is what the view looks like:


When I hit update, and it refreshes, the bio info is not there. Could someone please help me?


Thank you




Reply all
Reply to author
Forward
0 new messages