A
A
ahmed_al_asad2016-10-18 02:35:45
Django
ahmed_al_asad, 2016-10-18 02:35:45

Why can't I display the model in the template?

Using Django 1.9
I want to pull all the data from my database.
My Model:

class content(models.Model):
    title = models.CharField(max_length=255)
    subTitle = models.CharField(max_length=200)
    contactTitle = models.CharField(max_length=255)
    address = models.TextField(max_length=500)
    subAddress = models.TextField(max_length=300)

    def __str__(self):
        return self.title

My view:
def main(request):
    queryset = content.objects.all()
    context = {
        "content": queryset
    }
    return render(request, "content/index.html", context)

Template:
<div class="content-section-a">
      <div class="container">
         <div class="row">
            <div class="col-lg-5 col-sm-6">
               <hr class="section-heading-spacer">
               <div class="clearfix"></div>
               <h2 class="section-heading">{{ content.title }}</h2>
               <p class="lead">{{ content.address }}
                  <ul>
                    <li>{{ content.subAddress }}</li>
                  </ul>
               </p>
            </div>
            <div class="col-lg-5 col-lg-offset-2 col-sm-6">
               <img class="img-responsive" src="{% static 'content/img/logo.png' %}" alt="">
            </div>
         </div>
      </div>
      <!-- /.container -->
   </div>

Nothing is displayed on my page, it's completely empty. Tell me what I'm doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Сергей Горностаев, 2016-10-18
@ahmed_al_asad

В переменной queryset содержится массив объектов класса content, а в шаблоне вы работаете с данными так, будто там только одно значение. Либо вставьте в шаблон цикл, либо замените .all() на .first().

Z
zelsky, 2016-10-18
@zelsky

Название класов пишыте КемелКейсом.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question