Home > Net >  show database to template django
show database to template django

Time:07-08

hello I've been trying for a long time to make the database appear in the template but since then I can't show the database to the index.html I use version 4 of django with bootstrap thank you for showing me the solution because on my side I really tried everything but zero always empty in the final rendering on my visual

MY MODEL

from django.db import models



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")
   <div >
                <div >
                    <div  id="all_property">
                        <div >


                            <div >
                                <div >
                                    <div >
                                        <a  href="avendre"><img src="{% static 'images/property/property_1.jpg'%}" alt="#">
                                        </a>
                                        <ul >
                                            <li ><span>À VENDRE</span></li>
                                        </ul>
                                        <div >


                                        </div>
                                    </div>

                                    {% for appartement in Appartement %}
                                    <div >
                                        <h4><a href="avendre">{{ appartement.nom }}</a></h4>
                                        <div >
                                            <i ></i>
                                            <p>{{ Appartement.Ville_et_adresse}}</p>
                                        </div>
                                        <ul >
                                            <li> <i ></i>
                                                <span>{{ Appartement.NB_de_chambre }}</span>
                                            </li>
                                            <li> <i ></i>
                                                <span>{{ Appartement.NB_de_douche }}</span>
                                            </li>
                                            <li> <i ></i>
                                                <span>{{ Appartement.Superficie }}</span>
                                            </li>
                                            <li> <i ></i>
                                                <span>{{Appartement.NB_de_piscine }}</span>
                                            </li>
                                        </ul>
                                        <div >

                                            <a >
                                                <div >
                                                    <p>{{ Appartement.Prix }}</p>
                                                </div>
                                            </a>
                                        </div>
                                    </div>
                                    {% endfor %}
                                </div>
                            </div>






                        </div>
                    </div>

                </div>
            </div>
from django.urls import reverse
from django.shortcuts import render
from django.core.mail import send_mail
from django.views.generic import TemplateView
from django.http import HttpResponse
from django.core.mail import BadHeaderError, send_mail
from django.http import HttpResponse, HttpResponseRedirect
from django.conf import settings
from .models import Appartement


def index(request):
    appartement = Appartement.objects.all()
    context={'appartement':appartement}
    
    return render(request, 'index.html' , context)

CodePudding user response:

In your views you are sending all the objects in the context variable appartement. In the template you have used Appartement. Change this to

{% for appt in appartement %}

or change the context variable to appartements such that context={'appartements':appartement}, and then use in your template

{% for appartement in appartements %}

Then inside the for loop access each of the appartement instance like {{appartement.nom}} and so on.

CodePudding user response:

it is ok but image database dont show to the html

  • Related