我一直试图在我的index.html模板中显示一些数据,但没有结果。
models.py
class Appartement(models.Model):
nom = models.CharField(max_length=200)
Ville_et_adresse = models.CharField(max_length=200)
NB_de_chambre = models.DecimalField(max_digits=20, decimal_places=0)
NB_de_douche = models.DecimalField(max_digits=20, decimal_places=0)
NB_de_garage = models.DecimalField(max_digits=20, decimal_places=0)
NB_de_piscine = models.DecimalField(max_digits=20, decimal_places=0)
NB_de_cuisine = models.DecimalField(max_digits=20, decimal_places=0)
Superficie = models.DecimalField(max_digits=20, decimal_places=0)
Prix = models.DecimalField(max_digits=20, decimal_places=0)
Description = models.TextField(max_length=1000, default=True)
image = models.ImageField(upload_to="static/upload/", blank=True, null=True)
actif = models.BooleanField(default=True)
def __str__(self):
return f'{self.name}'
class Meta:
ordering = ['-id']
verbose_name_plural =("Appartements")views.py
from .models import Appartement
def index(request):
appartement = Appartement.objects.all()
context={'appartement':appartement}
return render(request, 'index.html' , context)index.html
<div class="property-item">
<a class="property-img" href="avendre">
<img src="{% static 'images/property/property_1.jpg'%}" alt="#">
</a>
...
</div>
{% for appartement in Appartement %}
<div class="property-title-box">
<h4><a href="avendre">{{ appartement.nom }}</a></h4>
<div class="property-location">
<i class="fa fa-map-marker-alt"></i>
<p>{{ Appartement.Ville_et_adresse}}</p>
</div>
<ul class="property-feature">
<li> <i class="fas fa-bed"></i>
<span>{{ Appartement.NB_de_chambre }}</span>
</li>
<li> <i class="fas fa-bath"></i>
<span>{{ Appartement.NB_de_douche }}</span>
</li>
<li> <i class="fas fa-arrows-alt"></i>
<span>{{ Appartement.Superficie }}</span>
</li>
<li> <i class="fas fa-car"></i>
<span>{{Appartement.NB_de_piscine }}</span>
</li>
</ul>
<div class="trending-bottom">
<a class="trend-left float-left">
<div class="trend-open">
<p>{{ Appartement.Prix }}</p>
</div>
</a>
</div>
</div>
{% endfor %}我在引导程序中使用django-4。
发布于 2022-07-07 03:18:50
在您的视图中,您将发送上下文变量appartement中的所有对象。在模板中使用了Appartement。将此更改为
{% for appt in appartement %}或将上下文变量更改为appartements,使之成为context={'appartements':appartement},然后在模板中使用
{% for appartement in appartements %}然后在for循环中访问每个appartement实例,如{{appartement.nom}}等等。
https://stackoverflow.com/questions/72891191
复制相似问题