I have a problem with adding a user to a group

i have this method that should signup a new user and that goes well should redirect to “etudiant_login” or if not , to “accueil”. The problem is when i try to add a member to the group “ETUDIANT” it redirects me to “accueil” . I couldnt find anything clear on the django docs on how to add to Groups
Any sort of help would be appreciated

def etudiant_inscription(request):

form = form_etudiant()

if request.method == 'POST':

    form = form_etudiant(request.POST)

    if form.is_valid():

        et = form.save(commit=False)


        group = Group.objects.get_or_create(name='ETUDIANT')


        return HttpResponseRedirect('/etudiant_login')


        return HttpResponseRedirect('/accueil')

return render(request,‘etudiant_inscription.html’,{‘form’:form})

class etudiant(models.Model):

    identif = models.CharField(primary_key=True,max_length=20)

    nom = models.CharField(max_length=20)

    prenom = models.CharField(max_length=20)

    date_naissance = models.DateField(blank=True,null=True)

    photo = models.ImageField(upload_to=None, height_field=None, width_field=None, max_length=100,blank=True,null=True)

    adresse = models.CharField(max_length=50)

    email = models.EmailField(max_length=50,null=True,blank=True)

    état = models.CharField(max_length=20)

    situation = models.CharField(max_length=20)

    password = models.CharField(max_length=20)

    def __str__(self):

            return self.identifiant

class form_etudiant(forms.ModelForm):

class Meta:

    model = models.etudiant

    fields = '__all__'

You’ve got a couple different problems here, but the first is that you’re just doing a redirect to accueil in the case of an invalid form. That’s not giving you any opportunity to see what the errors are on a form.

If a form is invalid, you want to rerender the form, with the form that was posted. See the example at Working with forms | Django documentation | Django.

Thank you for the tip , i’ll see to that asap
but do you have some advice on the Group part? I’ve looked everywhere and couldnt find anything helpful

The User model has a many-to-many relationship with Group. Working with that relationship is exactly the same as working with any other many-to-many relationship.

Start with reviewing Many-to-many relationships | Django documentation | Django.

Relative to this topic, the example uses models named Article and Publication. Since it’s the Article that has the ManyToMany field defined for Publication in the example, the parallel is that Article corresponds to User and Publication corresponds to Group. So you want to identify how a Publication is assigned to an Article, and use that as your pattern to figure out how to assign a Group to a User.