简体   繁体   中英

Problem in passing dictionary parameter to render template as Argument Django

I am trying to send a list of books objects to my template to display their names and images.Here is my book class

class Book(models.Model):
    title=models.CharField(max_length=100)
    zonar=models.CharField(max_length=20)
    book_id=models.IntegerField(default=10)
    image=models.ImageField()
    file=models.FileField(upload_to="cars",default="")

Here is my django view

def books_display(request,zonar):
    ###########
    ###########
    zonar_books=Book.objects.filter(zonar=zonar)
    books={"zonar":zonar,"zonar_books":zonar_books}

    return render(request,"books/books_listed.html",books)

finally this is my template

<<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>
      {% if zonar %}
      {{ zonar }}
      {%endif%}
       books </title>
  </head>
  <body>
    {% if zonar_books %}
    {% for book in zonar_books %}
    <h1>{{ book.tile}}</h1>
    {% endfor %}
    {% endif %}

  </body>
</html>

And iam getting following error

Error during template rendering
In template C:\Users\Sriram\Desktop\books_site\books\templates\books\books_listed.html, error at line 12

no such column: books_book.zonar

Try {{ book.title }} instead {{ book.tile}}.

It seems to me that you did not apply migrations

Try to run python manage.py makemigrations and then python manage.py migrate (you should know it)

And use {{ book.title }} , instead of {{ book.tile }}

It should work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM