--I have an issue that I have been battling for a long time Iwant to get data out from different related models. I have threemodels. Category, Author, Post. The Category model categories my postthat are on the Post model. The issue I am having is this, when I perform my loopon my template (i.e html file) I can get data out from my Post model but I can notget data out from my Category model. Please I need someone to help me out or tell mewhat I am doing wrong.below is my sample code for you to know what I am talking about.models.pyclass Category(models.Model):name = models.CharField(max_length=100)tagline = models.TextField()def __str__(self):return self.nameclass Author(models.Model):name = models.CharField(max_length=200)email = models.EmailField()def __str__(self):return self.nameclass Post(models.Model):category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True, related_name='my_category')headline = models.CharField(max_length=255)body_text = models.TextField()authors = models.ManyToManyField(Author)def __str__(self):return self.headline
views.pyfrom first_app.models import Author, Category, Postdef post_from_cat(request, cat_id):b = Category.objects.get(pk=cat_id)result = b.my_category.all()return render(request, 'first_app/index.html', {'key':result})on my app urls.pyfrom first_app import viewsurlpatterns = [path('', views.home_app, name='home_app'),path('post-cat/<int:cat_id>/', views.post_from_cat, name='post_from_cat'),]
on my index.html file{% if key %}{% for k in key %}<p><strong>Title</strong><br>{{ k.headline }}</p><p><strong>Body</strong><br>{{ k.body_text }}</p><hr>{% endfor %}{% else %}<p>No data</p>{% endif %}The resulting page on my browser
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3ddb6604-af4e-4eaf-b460-b55424d9c890%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANyc5fcCf9U81qSdmON7rf7X5f88SmSpOSgtsDELDq4ZAEfXow%40mail.gmail.com.