Django list rendered as string in template - cannot access list value
Question: New to Django so please bare with me.

_The problem :_ I try to iterate over a list which is the result of the
selection of multiple items in a form (use of forms.MultipleChoiceField). This
is then saved in my db using models.Charfield and passed from the view to the
template through context_dict `(context_dict['attachment'] =
attachment.pieces_jointes)` However I think that it is not saved as a list but
as a string. Instead of:

    
    
    [u'essai document', u'essai document 2', u'essai document5'], 
    

I think I have (because of model field type):

    
    
    "[u'essai document', u'essai document 2', u'essai document5']"
    

Indeed, in my template if I type :

    
    
    {% for attachment in attachments %}
        {{attachment.pieces_jointes.1}}
     {% endfor %}
    

I get for that record:

    
    
    u
    

and not :

    
    
    essai document 2
    

For your information, if I open the admin and look at the value recorded I
have :

    
    
    [u'essai document', u'essai document 2', u'essai document5']
    

I have tried to get rid of the possible "" which could be around the list but
I haven't succeeded. Can anyone explain my how I can turn that string back
into a list and iterate over it in my template? Thanks

A bit of code as requested:

models.py

    
    
    class Document (models.Model):
        dpseudo = models.ForeignKey(Identite)
        dnom = models.CharField(max_length=128, unique=False)
        dphoto = models.ImageField(upload_to='profile_document', blank=False)
        ddate_ajout = models.CharField(max_length=128, unique=False)
        slug = models.SlugField(unique=True)
        def save(self, *args, **kwargs):
            if not self.id:
                    self.slug = slugify(self.dpseudo) + slugify(self.dnom) + slugify(self.ddate_ajout)
            super(Document, self).save(*args, **kwargs)
    
        def __unicode__(self):  #For Python 2, use __str__ on Python 3
            return self.dnom
    
    
    
    class Attachment (models.Model):
        name = models.CharField(max_length=128, unique=True)
        date_creation = models.CharField(max_length=128, unique=False)
        statut = models.CharField(max_length=128, unique=False)
        pieces_jointes = models.CharField(max_length=128, unique=False)
        slug = models.SlugField(unique=True)
    
        def save(self, *args, **kwargs):
            if not self.id:
                    self.slug = slugify(self.name)
            super(Attachment, self).save(*args, **kwargs)
    
        def __unicode__(self):  #For Python 2, use __str__ on Python 3
            return self.name
    

views.py

    
    
    import ast
    
    def attacher(request):
        piece_list = Attachment.objects.all()
        attachment_list =[ast.literal_eval(i) for i in Attachment.objects.values_list('pieces_jointes', flat=True)]
    
        context_dict = {'pieces': piece_list, 'nom_pieces_jointes' : attachment_list}
    
        return render(request, 'job/attacher.html', context_dict)
    

forms.py

    
    
    class AttachmentForm(forms.ModelForm):
        name = forms.CharField(max_length=128, help_text="Please enter the name.")
        pieces_jointes = forms.MultipleChoiceField(choices=[])
    
        def __init__(self, *args, **kwargs):
            super(AttchmentForm, self).__init__(*args, **kwargs)
            self.fields['pieces_jointes'].choices = [(document.dnom, document.dnom) for document in Document.objects.all()]
    
        class Meta:
            model = Attchment
            fields = ('name','pieces_jointes')
    

As advised, I used ast.literal_eval() in my views but I get an error :

unexpected EOF while parsing (, line 1)


Answer: As you said you have saved `attachment.pieces_jointes` as string :

    
    
    "[u'essai document', u'essai document 2', u'essai document5']"
    

you can use `ast.literal_eval` in your `view` to pass the list in template:

    
    
    >>> import ast
    >>> l=ast.literal_eval("[u'essai document', u'essai document 2', u'essai document5']")
    >>> l[1]
    u'essai document 2'
    

