Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django模板呈现未给出预期结果_Python_Python 3.x_Django_Django Views_Django Templates - Fatal编程技术网

Python Django模板呈现未给出预期结果

Python Django模板呈现未给出预期结果,python,python-3.x,django,django-views,django-templates,Python,Python 3.x,Django,Django Views,Django Templates,我正在将视图中的数据渲染到模板中,如下所示: <tbody> {% for item in lyrics %} <tr class='lyrics-table'> <td>{{item}}</td> <td> {% if item in user_flash %} <p>{{flash}}</p> {% else %}

我正在将视图中的数据渲染到模板中,如下所示:

<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>
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
我认为使用
for
循环可以让我得到
flash.answer
for
user\u word
中的所有单词。在我正在测试的示例中,应该有两个单词,但我只得到一个。我做错了什么

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)
在代码中

for word in user_word:
    flash = Flashcard.objects.get(owner=self.request.user, question=word)
context['flash'] = flash.answer
context['flash']
将保存最后一个
flash.answer
,因为最后一行在for循环之外(它的缩进级别比第二行小一个)


你的意思可能是这样的吗

context['flash_list'] = []
for word in user_word:
    flash = Flashcard.objects.get(owner=self.request.user, question=word)
    context['flash_list'].append(flash.answer)

非常感谢。这现在给了我整个查询集,但我现在如何在模板中显示相关值(而不是整个查询集)?@MeL我不确定我是否理解这个后续问题。你能把你的模型添加到问题中,以便更好地了解情况吗?我已经添加了我的抽认卡模型。但如果你看一下我的后续问题,也许会更好,我希望在这里能更好地解释一下:。