0

I have make an override on my User class, i have add fields. I have make a template for update the fields of the User class. But the problem is i have only some fields having a data. "Firstname, Lastname"

This is my views for editing an user:

# Vue de la page User (édition)
@staff_member_required(login_url='user_login')
def admin_users_edit(request, pk):
    if request.method == "POST":
        user = User.objects.get(id=pk)
        form = UserEditForm(instance=user, data=request.POST)
        if form.is_valid():
            user.save()
            messages.add_message(request, messages.SUCCESS, "L'utilisateur à bien été modifié")
            return redirect('admin_users_index')
        else:
            print(form)
            print("Invalid Form")
            print(form.errors)
            return redirect('admin_departements_index')
    else:
        user = User.objects.get(id=pk)
        form = UserEditForm(instance=user)
        return render(request, 'back/users/edit.html', {"user":user,"form":form})

And this is my forms, i have try to render the checkox with different solutions, but the radio or checkbox dont populate with the data, they are empty on the form...

# Formulaire de l'édition d'un utilisateur
YES_OR_NO = (
    (True, 'Oui'),
    (False, 'Non')
)
class UserEditForm(forms.ModelForm):
    username = forms.CharField(label="", help_text="", widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder':'Pseudo','autocomplete':'off'}))
    first_name = forms.CharField(label="", help_text="", widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder':'Prénom','autocomplete':'off'}))
    last_name = forms.CharField(label="", help_text="", widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder':'Nom','autocomplete':'off'}))
    email = forms.CharField(label="", help_text="", widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder':'Pseudo','autocomplete':'off'}))
    password = forms.CharField(label="", help_text="", widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder':'Mot de passe','autocomplete':'off','required':'false'}))
    phone = forms.CharField(label="", help_text="", widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder':'Téléphone fixe','autocomplete':'off'}))
    mobile = forms.CharField(label="", help_text="", widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder':'Portable','autocomplete':'off'}))
    is_staff = forms.BooleanField(widget=CheckboxInput())
    is_active = forms.BooleanField(widget=RadioSelect(attrs={'class': 'radio-primary'}, choices=YES_OR_NO), required=True)
    newsletter = forms.BooleanField(widget=RadioSelect(attrs={'class': 'radio-primary'}, choices=YES_OR_NO), required=True)
    cgu = forms.BooleanField(widget=RadioSelect(attrs={'class': 'radio-primary'}, choices=YES_OR_NO), required=True)
    avatar = forms.FileField()
    class Meta:
        model = User
        fields = 'username','first_name','last_name','email','password',
        'phone','mobile','is_staff','is_active','newsletter','cgu',
        'avatar',

I have search on the docs of django with no success, i dont know why, the mobile is nos displying for example of the checkbox and the radio for 'is_staff', 'is_active' are empty...

Thank you.

Créatux
  • 9
  • 5

0 Answers0