Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
Django Split a charfield output which is seperated by ','(comma) into an array of strings.?

template.html

{{ profile.tag }}

views.py

class ProfileView(CanEditMixin, UserPassesTestMixin, DetailView):
   template_name = "profile/profile_view.html"
   queryset = User.objects.all()
   context_object_name = 'profile'
   slug_field = "username"`

gives output as "tag1,tag2,tag3"

is there ant way to do it in django itself?

i want output as shown belowrequired -output image

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I found a better way to solve the problem by adding additional function in the models.py file

models.py

def tags_list(self):
    return self.tags.split(',')

template.html

{% for tag in tags_list %}
    <p>{{ tag }}</p>
{% endfor %}

simple and easy!

over 4 years ago · Santiago Trujillo Relatório

0

In your view, split the comma value CharField field like this:

class ProfileView(CanEditMixin, UserPassesTestMixin, DetailView):
    template_name = "profile/profile_view.html"
    queryset = User.objects.all()
    context_object_name = 'profile'
    slug_field = "username"`

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)  # get parent context
        # the model here should be replaced with the object that has the "tag" attribute. DO NOT use the "model" word.
        # Try self.object.tag.split(',')
        tag_list = model.tag.split(',')  # conversion from a string to a list
        context['tag_list'] = tag_list  # add to the context the tag_list list
        return context

and then in your template simply iterate over it:

{% for tag in tag_list %}
    <p>{{ tag }}</p>
{% endfor %}
over 4 years ago · Santiago Trujillo Relatório

0

The correct way works, but I would use the list built-in method to just change the string from array.

class MyModel(models.Model):
    string_array = models.CharField(max_length=100)

    def pass_to_list(self):
        return list(self.string_array)

Notes:

  • Remember that your string array must be an actual array on the database, with '[' and everything
  • This way works by taking use of the built-in list method, but you can also just return the string splitted by commas
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda