Answer the question
In order to leave comments, you need to log in
Can't access a field in a model in a Django template?
I need to check if the user is in the list of those who bookmarked the quiz, then display one, otherwise - the other.
Here is the bookmark model:
class Bookmark(models.Model):
"""Bookmar for a quiz"""
quiz = models.ForeignKey(
Quiz,
on_delete=models.PROTECT,
verbose_name='Викторина',
related_name='bookmarks'
)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
verbose_name='Пользователь',
related_name='bookmarks'
)
<span class="quiz__bookmarks ml-3">
{% if user in quiz.get_bookmarks_users %}
<i class="fas fa-star bookmarked-star"></i>
{% else %}
<i class="far fa-star"></i>
{% endif %}
{{ quiz.get_bookmarks_count }}
</span>
def get_bookmarks_users(self):
return self.bookmarks.all().user
Answer the question
In order to leave comments, you need to log in
One option:
views.py:
...
bookmarks = Bookmark.all()
context['bookmarks'] = bookmarks
...
<span class="quiz__bookmarks ml-3">
{% for bookmark in bookmarks %}
{% if user in bookmark.user %}
<i class="fas fa-star bookmarked-star"></i>
{% else %}
<i class="far fa-star"></i>
{% endif %}
{% endfor %}
{{ quiz.get_bookmarks_count }}
</span>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question