Vous n'êtes pas identifié(e).
Bonjour,
Je souhaite faire un formulaire de recherche basé sur plusieurs modeles.
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
Fichier views.py
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})
Voici mon fichier resultat_recherche.html
{% 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 %}
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
Merci de votre aide !!
Dernière modification par cobra85 (03-05-2017 18:35:54)
Hors ligne