You see, in my project I need to use manytomany variables to store foreign keys that point to several values.
Code on models.py:
class desplazamiento(models.Model):
nombre=models.CharField(max_length=15)
descripcion=RichTextField(max_length=150)
foto=models.ImageField(null=True)
def __str__(self):
return self.nombre
class animal(models.Model):
ncomun=models.CharField(max_length=50)
ncientifico=models.CharField(max_length=50)
foto=models.ImageField()
categoria=models.ForeignKey(categoria,null=True)
alimentacion=models.ForeignKey(alimentacion,null=True)
desplazamiento=models.ManyToManyField(desplazamiento, blank=True)
def __str__(self):
return self.ncomun
Code in views.py:
def datos_animal(request,i):
try:
ani=animal.objects.get(pk=i)
except animal.DoesNotExist:
ani=None
return render(request,"indice.html",{"ani":ani})
HTML code:
<body>
<h1 style="text-align: center;">Más detalles acerca del animal</h1>
<img style="width: 250px; height: 200px; position:relative; left: 40%;" src="{% static ani.foto %}" />
<ul>
<li>Nombre comun: {{ani.ncomun}}</li>
<li>Nombre cientifico: {{ani.ncientifico}}</li>
<li>Categoria: {{ani.categoria.nombre}}</li>
<li>Alimentación: {{ani.alimentacion.nombre}}</li>
</ul>
<h2>Metodos de desplazamiento:</h2>
<ul>
{% for u in ani.deplazamiento.all %}
<li>{{u}}</li>
{% endfor %}
</ul>
<button style="position: absolute; left: 44%; bottom: 10%;" onclick="viaje('javascript:history.back(-1)')">Retroceder</button>
</body>
However, although when checking in admin the animal has its category, it is not shown in this view. What can I be doing wrong?