Djando bootstrap form, 'registrer' object has no attribute 'visible_fields'

Good morning everyone.

I’m new in django. I use django for the realization of a project to create an exam management application. I wanted to make a registration form using django bootstrap. Unfortunately, I have received an error which I have not been able to resolve for a few days. I have searched the forums but without success. The error is as follows:
’registrer’ object has no attribute 'visible_fields’
I give you my code juste in case :
in forms.py

class registrer(CreateView):
    model = Personnes
    template_name = 'registrer.html'
    form_class  = Personnes
    def clean(self):
        super().clean()
        Email = self.cleaned_data.get("Email")
        Nom = self.cleaned_data.get("Nom")
        Prenom = self.cleaned_data.get("Prenom")
        DatedateNaissance = self.cleaned_data.get("DateNaissance")
        LieuNaissance = self.cleaned_data.get("LieuNaissance")
        Tel = self.cleaned_data.get("Tel")
        Password = self.cleaned_data.get("Password")

In registrer.html

{% extends "app/layout.html" %}

{% block content %}
<h2>{{ title }}</h2>

{% load bootstrap %}


<form action="/register/" method="post">
    {% csrf_token %}
    
    {{ form|bootstrap }}

    <button type="submit" class="btn btn-success">Save person</button>

</form>  
{% endblock %}

{% block scripts %}
    
{% endblock %}

In views.py :

def register(request):
    assert isinstance(request, HttpRequest)
    if request.method == 'POST' :
        form = registrer(request.POST)
        if form.is_valid():
            clean(form)
            contact = Personnes.objects.filter(personne_email=Email)
            if contact.exist():
                message = "L'adresse email entrée existe déjà"
                return HttpResponse(message)
            else :
                candidat = Candidats.objects.create(Nom, Prenom, DateNaissance, LieuNaissance, Email, Tel, 255155, 0, 10)
                candidat.save()
                return HttpResponseRedirect('/')

    else :
        form = registrer()
    return render(request, 'app/registrer.html', {'form': form})

Please help me.

A few comments:

  1. In your forms.py file, why are you using CreateView for a form? Did you accidentally post the wrong code?
  2. In your views.py file, I’d be careful with using an assert statement. If you ever encounter a situation where the request parameter is not an HttpRequest then your site will crash and you won’t be able to properly supply an error code for the user. I would suggest using a try/except block instead.

If you pasted the wrong code for your forms.py file, please update the original post and let us know so we can take another look :slight_smile:

Thank you for answering @Suttonium. As I said, I’m a beginner, that’s why I’ve done this kind of error. So let me change this and I’ll get back to you. I’ll also use try/except block as you said. Thanks for your advice

1 Like

Take a look at the documentation for Django ModelForms :slight_smile: I think they’re what you’re looking for in this situation!

1 Like