Essex
Essex

Reputation: 6138

Django class-based view doesn't display query result

I'm trying to improve my Django view with classes in order to get a better script.

I don't why but I don't overcome to display query result with the new syntax. Maybe someone could help me to find a solution ?

This is my view :

class IdentityIndividuForm(TemplateView) :

    template_name= "Identity_Individu_Form.html"
    model = Individu


    def ID_Recherche (request) :

        if 'recherche' in request.GET:

            query_Nom_ID = request.GET.get('q1NomID')
            query_Prenom_ID = request.GET.get('q1PrenomID')
            query_DateNaissance_ID = request.GET.get('q1DateNaissanceID')
            query_VilleNaissance_ID = request.GET.get('q1VilleNaissanceID')

            sort_params = {}

            Individu_Recherche.set_if_not_none(sort_params, 'Nom__icontains', query_Nom_ID)
            Individu_Recherche.set_if_not_none(sort_params, 'Prenom__icontains', query_Prenom_ID)
            Individu_Recherche.set_if_not_none(sort_params, 'DateNaissance__icontains', query_DateNaissance_ID)
            Individu_Recherche.set_if_not_none(sort_params, 'VilleNaissance__icontains', query_VilleNaissance_ID)

            query_ID_list = Individu_Recherche.Recherche_Filter(Individu, sort_params)

            context = {
                "query_Nom_ID" : query_Nom_ID,
                "query_Prenom_ID" : query_Prenom_ID,
                "query_DateNaissance_ID" : query_DateNaissance_ID,
                "query_VilleNaissanceID" : query_VilleNaissance_ID,
                "query_ID_list" : query_ID_list,
            }

            return render(request, 'Identity_Individu_Form.html', context)

My url.py file :

urlpatterns = [
    url(r'^Formulaire/Individus$', IdentityIndividuForm.as_view(), name="IndividuFormulaire"),
]

And my template :

<div class="subtitle-form">
        <h4> <span class="glyphicon glyphicon-user"></span></span> Rechercher le n° identification d'un individu <a><span title="Outil permettant de vérifier si un individu est déjà enregistré dans la Base de Données Nationale. Saisir au minimum Nom et Prénom (entièrement ou en partie). Si la personne recherchée est trouvée, ne pas remplir le formulaire de création de fiche !"
        class="glyphicon glyphicon-info-sign"></a>
        </h4>
    </div>

    <div class="form">
        <form autocomplete="off" method="GET" action="">
            <input type="text" name="q1NomID" placeholder="Nom (ex:TEST) " value="{{ request.GET.q1NomID }}"> &nbsp;
            <input type="text" name="q1PrenomID" placeholder="Prénom (ex:Test)" value="{{ request.GET.q1PrenomID }}"> &nbsp; <p></p>
            <input id="id_search" type="text" name="q1DateNaissanceID" placeholder="Date de Naissance (YY-mm-dd) " value="{{ request.GET.q1DateNaissanceID }}"> &nbsp; <p></p>
            <input id="id_search" type="text" name="q1VilleNaissanceID" placeholder="Ville de Naissance" value="{{ request.GET.q1VilleNaissanceID }}"> &nbsp; <br></br>
            <input class="button" type="submit" name='recherche' value="Rechercher">&nbsp;
        </form>

        <br></br>

        <table style="width:120%">
            <tbody>
                <tr>
                    <th>ID</th>
                    <th>État</th>
                    <th>N° Identification</th>
                    <th>Civilité</th>
                    <th>Nom</th>
                    <th>Prénom</th>
                    <th>Date de Naissance</th>
                    <th>Ville de Naissance</th>
                    <th>Pays de Naissance</th>
                </tr>
                {% for item in query_ID_list %}
                <tr>
                    <td>{{ item.id}}</td>
                    <td>{{ item.Etat}}</td>
                    <td>{{ item.NumeroIdentification}}</td>
                    <td>{{ item.Civilite }}</td>
                    <td>{{ item.Nom }}</td>
                    <td>{{ item.Prenom }}</td>
                    <td>{{ item.DateNaissance }}</td>
                    <td>{{ item.VilleNaissance }}</td>
                    <td>{{ item.PaysNaissance.name }}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

Something is false in my script ? First time I'm trying to use Class Oriented Object.

Thank you !

Upvotes: 0

Views: 88

Answers (1)

user8060120
user8060120

Reputation:

You need rename your method and change return

from

def ID_Recherche(request) :
    # YOUR CODE HERE
    return render(request, 'Identity_Individu_Form.html', context)

to

def get_context_data(self, **kwargs):
    request = self.request # Or just override it in your code
    # YOUR CODE HERE
    return context

Upvotes: 1

Related Questions