Reputation: 1269
I am rendering data from my views into my template, as follows:
<tbody>
{% for item in lyrics %}
<tr class='lyrics-table'>
<td>{{item}}</td>
<td>
{% if item in user_flash %}
<p>{{flash}}</p>
{% else %}
<p>xxx</p>
{% endif %}
</td>
{{item}}
works as expected, but {{flash}}
only gives the same value for every row, instead of the relevant value.
My views are as follows:
class SongVocab(LoginRequiredMixin, generic.DetailView):
model= models.Song
template_name = 'videos/song_vocab.html'
context_object_name = 'song'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
from pymystem3 import Mystem
m = Mystem()
user_flash = Flashcard.objects.filter(owner=self.request.user).values_list('question', flat=True)
lyrics_list = models.Song.objects.get().lyrics_as_list()
user_flash_ = [item.replace('\n', ' ') for item in m.lemmatize(" ".join(user_flash))]
user_flash_clean = [w for w in user_flash_ if w.strip()] ##removes empty strings
lyrics_list_ = [item.replace('\n', ' ') for item in m.lemmatize(" ".join(lyrics_list))]
lyrics_list_clean = [w for w in lyrics_list_ if len(w.strip())]
user_word = list(set(user_flash_clean) & set(lyrics_list_clean))
import icu # PyICU
def sorted_strings(strings, locale=None):
if locale is None:
return sorted(strings)
collator = icu.Collator.createInstance(icu.Locale(locale))
return sorted(strings, key=collator.getSortKey)
context['percent_known'] = ((len(user_word))/(len(set(lyrics_list_clean))))*100
context['lyrics'] = sorted_strings(set(lyrics_list_clean),"ru_RU.UTF8")
context['user_flash'] = user_flash_clean
for word in user_word:
flash = Flashcard.objects.get(owner=self.request.user, question=word)
context['flash'] = flash.answer
return context
I thought that using the for
loop would let me get flash.answer
for all words in user_word
. In the example I'm testing, there should be two words, but I get just one. What am I doing wrong?
Models.py:
class Flashcard(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
deck = models.ForeignKey(Deck, on_delete=models.CASCADE)
question = models.TextField()
answer = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
last_shown_at = models.DateTimeField(auto_now_add=True)
next_due_date = models.DateTimeField(default=timezone.now)
difficulty = models.FloatField(default=2.5)
consec_correct_answers = models.IntegerField(default=0)
objects = FlashcardManager()
def __str__(self):
return self.question
def number_of_questions(self):
return self.question.count(deck=deck.id)
Upvotes: 1
Views: 81
Reputation: 16485
In your code
for word in user_word:
flash = Flashcard.objects.get(owner=self.request.user, question=word)
context['flash'] = flash.answer
context['flash']
will hold the last flash.answer
, because the last line is outside the for-loop (it has one indentation level less than the second line).
Did you perhaps mean something like this?
context['flash_list'] = []
for word in user_word:
flash = Flashcard.objects.get(owner=self.request.user, question=word)
context['flash_list'].append(flash.answer)
Upvotes: 1