Hi there,
I have recently started building a vocabulary app and was amazed by the competence and helpfulness of this community so that I came back today with another issue
I have this CBVs
class LanguageWordListView(ListView):
def get(self, request, language):
words = Word.objects.all().filter(language__name= language)
context = {
'words': words,
'language':language
}
return render(request, 'vocabTrainer/word_list.html', context)
class WordListView(ListView):
def get(self, request):
words = Word.objects.all()
return render(request, 'vocabTrainer/word_list.html', {'words':words})
and the following template:
<h1> Hi from the <span>{{ language }}</span> word list</h1>
<ul>
{% for word in words %}
<li><a href="{% url 'word-detail' word.pk %}">{{ word.word.abstract_word }}, {{ word.language.name }}</a></li>
{% endfor %}
</ul>
Both CBVs should roughly do the same - render a selection of words, while WordListView should render all words, the LanguageWordListView should only render those of a certain language, thus I filter all the words for language and get the intended result for {{ language }} but not for the words.
I assume this is because filter-method changes the objectâs structure somehow.
in the console I tried the following:
word = Word.objects.all()
test = word.filter(language__name = 'german')
for word in test:
print(word.text)
and I get the intended âMorgenâ or when I print word.word I get âmorningâ (which is my âbase wordâ)
So I cannot see what might be wrong here âŚ
any ideas?
Should I use two different templates? seems kinda âtoo muchâ, given how similar the purpose of these is.
edit: left out an important part of the template.
as requested, the models.py:
class AbstractWord(models.Model):
'''Abstract word model that is the common ground for other words, contains the word_id and the english version of the word
as an abstract representation of the word.
'''
word_id = models.BigAutoField(primary_key=True)
abstract_word = models.CharField(max_length=100, blank=False, default="", unique=True)
def __str__(self):
return f'{self.word_id}, {self.abstract_word}'
class Language(models.Model):
'''The language(s) that are supported for translations
'''
name = models.CharField(max_length=30)
def __str__(self):
return f'{self.name}'
class Word(models.Model):
'''The translated representations of each AbstractWord
word = the concept behind abstract
text = the word in the language we are looking for.
'''
word = models.ForeignKey(AbstractWord, to_field='abstract_word', on_delete=models.CASCADE)
language = models.ForeignKey(Language, on_delete=models.CASCADE)
text = models.CharField(max_length=100, blank=True)
definition = models.CharField(max_length=100, blank=True)
def __str__(self):
return f'{self.word}, {self.language}, {self.text}, {self.definition}'
and additionally the urls.py
urlpatterns = [
path('', IndexView.as_view(), name='index'),
path('word-list/', WordListView.as_view(), name='word-list-view'),
path('word-list/<str:language>/', LanguageWordListView.as_view(), name='lang-word-list-view'),
path('word-list/<str:language>/<int:pk>', WordDetailView.as_view(), name="word-detail"),
path('word-list/<int:pk>', WordDetailView.as_view(), name="word-detail"),
]