Solution 1 :

title is not the title, it is a References object, it is only because you implemented __str__ to return the title, that {{ title }}, will indeed render the .title attribute of that References object.

You thus iterate over it, and access the attribute:

{% for reference in post.reference_title.all  %}
    <a href="{{ }}">
        <li>{{ reference }}</li>
{% endfor %}

You thus can replace {{ reference }} with {{ reference.title }} here, although the two are equivalent because the __str__ returns the title.

Problem :

Brand new to Django/Python and thought I’d start by building a simple blog app. I would like the User to be able to post book references that include the book name and a link to the book.

My current References class in my looks like this:

class References(models.Model):
    link = models.URLField(max_length=150, default=False)
    title = models.CharField(max_length=100, default=False)

    def __str__(self):
        return self.title

and my Post class looks like this:

class Post(models.Model):
    title = models.CharField(max_length=31)
    content = models.TextField()
    thumbnail = models.ImageField()
    displayed_author = models.CharField(max_length=25, default=True)
    shortquote = models.TextField()
    reference_title = models.ManyToManyField(References)
    publish_date = models.DateTimeField(auto_now_add=True)
    last_updated = models.DateTimeField(auto_now=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    slug = models.SlugField()

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("detail", kwargs={
            'slug': self.slug

    def get_love_url(self):
        return reverse("love", kwargs={
            'slug': self.slug

    def comments(self):
        return self.comment_set.all()

    def get_comment_count(self):
        return self.comment_set.all().count()

    def get_view_count(self):
        return self.postview_set.all().count()

    def get_love_count(self):
        return self.love_set.all().count()

I understand that i’m only returning the title in my References class, I’ve tried returning self.title + self.linkbut this gives me both the title and link together when being called in the template.

My template calling the references class looks like this:

{% for title in post.reference_title.all  %}
        <a href="{{ link }}">
           <li>{{ title }}</li>
{% endfor %}

I’ve tried a combination of different things in order to get the link AND title to render independently as shown in the template, but the issue comes with knowing how to display different items from a class through a ManyToManyField. Any help on this would be great as I do believe it’s just something I haven’t learnt yet. Thanks in advance!