[Answered ]-Django trying to split and urlify tags in models, but code isn't working

2πŸ‘

βœ…

In split_tags function you are only iterating alltags without saving result from urlify. You can do it in one line with list comprehension:

def split_tags(self):
    return [urlify(tag) for tag in self.tags.split(', ')]

Your version need some changes:

def split_tags(self):
    alltags =  self.tags.split(', ')
    result = []
    for tag in alltags:
        result.append(urlify(tag))
    return result

Note that there is similar to your urlify function in django utils – slugify:

slugify()
Converts to lowercase, removes non-word characters
(alphanumerics and underscores) and converts spaces to hyphens. Also
strips leading and trailing whitespace.

πŸ‘€ndpu

Leave a comment