models.py
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
# on_deleting user, profile will also be deleted
image = models.ImageField(default="profilepic.jpg",upload_to="profile_pictures")
dob = models.DateField(null=True)
bio = models.TextField(null=True)
anonymous = models.BooleanField(default=False)
def __str__(self):
return f'{self.user.username} Profile'
views.py
@login_required
def profile_edit(request):
if request.method == 'POST':
u_form = UserUpdateForm(request.POST, instance=request.user)
p_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile)
if u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
messages.success(request, f'your account has been updated')
return redirect('users:profile')
else:
u_form = UserUpdateForm(instance=request.user)
p_form = ProfileForm()
context = {
'u_form' : u_form,
'p_form' : p_form
}
return render(request, 'users/profile_edit.html',context)
в users / profile_edit. html
<div class="form-group">
<label class="radio-inline"><input type="radio" name="anonymous" value='True'>Anonymous</label>
<label class="ml-5 radio-inline"><input type="radio" name="anonymous" value='False' checked>Non-Anonymous</label>
</div>
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Profile
class UserUpdateForm(forms.ModelForm):
class Meta:
model = User
fields = ['first_name','last_name']
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['image','dob','bio']
остальная часть формы работает нормально (обновляет содержимое профиля, как и ожидалось), но BooleanField для Аноним не работает, когда переключатели нажимаются перед отправкой формы