0

I am storing tags as a string seprated by commas in a model and i am trying to print those in template but it is not working I tried Django templates - split string to array answer but it is also not working nothing is printed and the function tags_as_list is also not running

model

class News(models.Model):
 link = models.TextField()
 headline = models.TextField()
 summary = models.TextField()
 tags = models.TextField(blank=True)
 time_date = models.DateTimeField(auto_now_add=True)

def tags_as_list(self):
    print("tagsss")
    return self.tags.split(',')

template

{% for tag in news_item.tags.tags_as_list %}
{{ tag }}

{% endfor %}
kaku
  • 39
  • 1
  • 7

2 Answers2

0

The method tags_as_list you defined is correct, however in the template you are not calling it correctly.

Assuming news_item is an instance of News you should call news_item.tags_as_list as the method is not on the tags, but on the model.

Tommaso Amici
  • 55
  • 3
  • 8
0

I was not using the function properly it directly gets access to the feild you don't have to mention it by yourself

{% for tag in news_item.tags_as_list %}
{{ tag }}
{% endfor %}
kaku
  • 39
  • 1
  • 7