Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

409
Views
Django divide una salida de campo de caracteres que está separada por ',' (coma) en una matriz de cadenas.

plantilla.html

 {{ profile.tag }}

vistas.py

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

da salida como "etiqueta1, etiqueta2, etiqueta3"

¿Hay alguna forma de hacerlo en Django?

quiero salida como se muestra a continuación requerido -imagen de salida

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Encontré una mejor manera de resolver el problema agregando una función adicional en el archivo models.py

modelos.py

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

plantilla.html

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

¡simple y fácil!

about 4 years ago · Santiago Trujillo Report

0

En su opinión, divida el campo CharField de valor de coma de esta manera:

 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

y luego en su plantilla simplemente itere sobre ella:

 {% for tag in tag_list %} <p>{{ tag }}</p> {% endfor %}
about 4 years ago · Santiago Trujillo Report

0

La forma correcta funciona, pero usaría el método integrado de list para simplemente cambiar la cadena de la matriz.

 class MyModel(models.Model): string_array = models.CharField(max_length=100) def pass_to_list(self): return list(self.string_array)

Notas:

  • Recuerde que su matriz de cadenas debe ser una matriz real en la base de datos, con '[' y todo
  • De esta manera funciona utilizando el método de list incorporado, pero también puede devolver la cadena dividida por comas
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!