I am struggling with this
{% extends "base_generic.html" %}
{% block content %}
<h1>{{ author.first_name }} {{ author.last_name }}</h1>
{% for book in view.books_by_author %}
{% if author.last_name in book.author %}
<p>{{ book.title }}</p>
{% endif %}
{% endfor %}
{% endblock %}
"author" is a context variable. This is books_by_author function:
def books_by_author(self):
books = Book.objects.all()
return books
This portion is not working:
{% if author.last_name in book.author %}
<p>{{ book.title }}</p>
But when I tried this, it's working. Is there a way to make "book.author" a string or is there a way around?
{% if "Twain" in book.author %}
<p>{{ book.title }}</p>
答案 0 :(得分:2)
You should compare with the last_name
of the related author
{% if author.last_name in book.author.last_name %}
<p>{{ book.title }}</p>
{% endif %}
Or more strictly, use ==
:
{% if author.last_name == book.author.last_name %}
<p>{{ book.title }}</p>
{% endif %}