Manytomany field returns empty list
I'm new to django so may not know smth but i've searched through google and haven't got any answer. I have such models:
class Word(models.Model):
word = models.CharField(max_length=100)
img = models.ImageField(upload_to='img/', blank=True, null=True)
audio = models.FileField(upload_to='audio/', blank=True, null=True)
language = models.ForeignKey(Language)
def __unicode__(self):
return self.word
class Lesson(models.Model):
title = models.CharField(max_length=100)
language = models.ForeignKey(Language)
lesson_words_list = models.ManyToManyField(Word)
def __unicode__(self):
return self.title
And i have next function in views:
def view_lesson(request, language, lesson_title):
cur_lesson = Lesson.objects.filter(title__iexact = lesson_title).get(language__name__iexact=language)
words_list = cur_lesson.lesson_words_list.all()
return render_to_response("view_lesson.html", {"language":language, "lesson_title": lesson_title, "words_list":words_list})
here is how i call it in the template:
{% for Word in words_list %}
{{ Word.word }}
{% endfor %}
The problem is words_list 开发者_Go百科is empty while through the admin i clearly see that my lesson has related words. Why could it be empty? and where should i search for an answer?
Is
{{ cur_lesson }}
showing the correct lesson in your template?
Try:
{% for word in cur_lesson.lesson_word_list.all %}
{{ word }}
{% endfor %}
Also see if
{{ cur_lesson.lesson_word_list.all }}
show the correct list of words in your template
精彩评论