Bonjour,
Je souhaite faire un formulaire de recherche basé sur plusieurs modeles.
Fichier views.py
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 class Adresse_ip(models.Model): ... nom = models.CharField(max_length=30, unique=True) pays = models.ForeignKey(Pays,blank=True, null=True) ... class Meta: ordering = ['nom'] def __unicode__(self): return self.nom class Nom_domaine(models.Model): ... nom = models.CharField(max_length=30, unique=True) hebergeur = models.CharField(max_length=15, blank=True) ... class Meta: ordering = ['nom'] def __unicode__(self): return self.nom
Voici mon fichier resultat_recherche.html
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 def recherche(request): if request.method == 'POST': recherche_form = RechercheForm(request.POST) if recherche_form.is_valid(): sujet = recherche_form.cleaned_data['sujet'] try: resultat = Adresse_ip.objects.filter(nom=sujet) or Nom_domaine.objects.filter(nom=sujet) except ObjectDoesNotExist: return HttpResponseRedirect(reverse('liste')) return render(request, 'resultat_recherche.html', {'resultat_recherche': resultat}) else: recherche_form = RechercheForm() return render(request, 'recherche.html', {'recherche_form': recherche_form})
Le probleme est que le résultat s'affiche en double bien qu'il ne soit présent que dans un seul des deux modeles
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 {% extends "base.html" %} {% load staticfiles %} {% block content %} <h1>Resultat de la recherche</h1> <div class="container-fluid"> {% if resultat_recherche %} <div id="wrapper"> <table cellpadding="0" cellspacing="0" border="0" class="sortable"> <thead> <tr> <th>nom</th> <th>...</th> </tr> </thead> {% for nom_domaine in resultat_recherche.all %} <tbody> <tr> <td>{{ nom_domaine.nom }}</td> <td>{{ ... }}</td> </tr> </tbody> {% endfor %} {% for adresse_ip in resultat_recherche.all %} <tbody> <tr> <td>{{ adresse_ip.nom }}</td> <td>{{ ... }}</td> </tr> </tbody> {% endfor %} </table> </div> {% else %} Il n'y a pas de valeur dans la base, veuillez refaire une <a href="{% url 'recherche' %}">{{ 'recherche' }}</a> {% endif %} </div> {% endblock %}
Merci de votre aide !!
Partager