'Inclusion_tag doesn't display data in other models on template
I was follow to a old courses Udemy and I try create a project had 2 app( group & posts) and just found out that you can use:
from django import template
register = template.Library()
To allow link models app to another app. In the old courses it just add in models.py and create tag in post_list.html to show what group you join and all group but don't create templatetags folder and how it work. Even when I check the sample it don't had any thing. So I was search gg try build it by myself, but it doesn't show anything. Can you check it. Thank
App groups models.py:
class Group(models.Model):
name = models.CharField(max_length=235, unique=True)
slug = models.SlugField(allow_unicode=True,unique=True)
descripsion = models.TextField(blank=True,default='')
descripsion_html = models.TextField(editable=False,default='',blank=True)
member = models.ManyToManyField(User,through='GroupMember')
def __str__(self):
return self.name
def save(self,*args, **kwargs):
self.slug =slugify(self.name)
self.descripsion_html = misaka.html(self.descripsion)
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse("groups:single", kwargs={"slug": self.slug})
class GroupMember(models.Model):
group = models.ForeignKey(Group,related_name='memberships',on_delete=models.CASCADE)
user = models.ForeignKey(User, related_name='user_groups',on_delete=models.CASCADE)
def __str__(self):
return self.user.username
In templatetags group_tags.py:
from django import template
from ..models import Group,GroupMember
register = template.Library()
@register.inclusion_tag('clonemedia/posts/templates/posts/post_list.html')
def get_other_group():
group_members = GroupMember.objects.all()
other_groups = Group.objects.all()
return {'other_group':other_groups, 'group_member':group_members }
In template post_list.html:
{% load group_tags %}
<h5 class='title'> Your Groups</h5>
<ul class='list-unstyled'>
{% for member in group_member %}
<li class='group li-with-bullet'>
<a href="{% url 'groups:single' slug=member.group.slug %}">{{ member.group.name }}</a>
</li>
{% endfor %}
<h5 class='title'> All groups</h5>
<ul class='list-unstyled'>
{% for other in other_group %}
<li class="group li-with-bullet"><a href="{% url 'groups:single' slug=other.slug %}">{{ other.name }}</a></li>
{% endfor %}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
