In this specific part of my Django project, I am trying to return a dict containing QuerySet of objects that I have filtered through so that these objects can be used in the template they are passed to. Below is my code:
In views.py
def shows_in_category(request, category_slug): category = get_object_or_404(Category, slug=category_slug) print(category_slug) shows = theShow.objects.filter(category__name=category).all() print(shows) return render(request, 'show/show_list_view.html', {'shows': shows})
In models.py
class Category(models.Model): name = models.CharField(max_length=255, db_index=True) slug = models.SlugField(max_length=255, unique=True) class Meta: verbose_name_plural = 'Categories' def __str__(self): return self.name def get_absolute_url(self): return reverse("theshowapp:shows_in_category", args=[self.slug]) class theShow(models.Model): english_name = models.CharField(max_length=400) show_type = models.CharField(max_length=200, blank=True) category = models.ManyToManyField(Category) slug = models.SlugField(max_length=400,unique=True) class Meta: verbose_name_plural = 'Shows Series' def __str__(self): return self.english_name
Here is the full traceback:
Traceback (most recent call last): File "C:\Users\danie\miniconda3\lib\site- packages\django\core\handlers\exception.py", line 47, in inner response = get_response(request) File "C:\Users\danie\miniconda3\lib\site- packages\django\core\handlers\base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\danie\OneDrive\Documents\streamingsitetest\showing\views.py", line 14, in shows_in_category return render(request, 'show_templates/show_list_view.html', {'shows': shows}) File "C:\Users\danie\miniconda3\lib\site-packages\django\shortcuts.py", line 19, in render content = loader.render_to_string(template_name, context, request, using=using) File "C:\Users\danie\miniconda3\lib\site- packages\django\template\loader.py", line 62, in render_to_string return template.render(context, request) File "C:\Users\danie\miniconda3\lib\site- packages\django\template\backends\django.py", line 61, in render return self.template.render(context) File "C:\Users\danie\miniconda3\lib\site-packages\django\template\base.py", line 170, in render return self._render(context) File "C:\Users\danie\miniconda3\lib\site-packages\django\template\base.py", line 162, in _render return self.nodelist.render(context) File "C:\Users\danie\miniconda3\lib\site-packages\django\template\base.py", line 938, in render bit = node.render_annotated(context) File "C:\Users\danie\miniconda3\lib\site-packages\django\template\base.py", line 905, in render_annotated return self.render(context) File "C:\Users\danie\miniconda3\lib\site- packages\django\template\loader_tags.py", line 150, in render return compiled_parent._render(context) File "C:\Users\danie\miniconda3\lib\site-packages\django\template\base.py", line 162, in _render return self.nodelist.render(context) File "C:\Users\danie\miniconda3\lib\site-packages\django\template\base.py", line 938, in render bit = node.render_annotated(context) File "C:\Users\danie\miniconda3\lib\site-packages\django\template\base.py", line 905, in render_annotated return self.render(context) File "C:\Users\danie\miniconda3\lib\site- packages\django\template\loader_tags.py", line 62, in render result = block.nodelist.render(context) File "C:\Users\danie\miniconda3\lib\site-packages\django\template\base.py", line 938, in render bit = node.render_annotated(context) File "C:\Users\danie\miniconda3\lib\site-packages\django\template\base.py", line 905, in render_annotated return self.render(context) File "C:\Users\danie\miniconda3\lib\site- packages\django\template\defaulttags.py", line 211, in render nodelist.append(node.render_annotated(context)) File "C:\Users\danie\miniconda3\lib\site-packages\django\template\base.py", line 905, in render_annotated return self.render(context) File "C:\Users\danie\miniconda3\lib\site- packages\django\template\defaulttags.py", line 167, in render values = list(values) Exception Type: TypeError at /shows/action Exception Value: 'ManyRelatedManager' object is not iterable
I understand from previous questions similar to mine that I needed to make shows
a QuerySet so that it is iterable which is why I added .all()
to the end of its definition. I used print(type(shows)) and ensured that a QuerySet was created and I'm still receiving this error. What can I do to make it so that I can iterate through shows
?
没有评论:
发表评论