Pass data to javascript from views.py

Hello everyone,

There are some codes in between, but I wanted to show the specific line below. I’m sending the project_list data to base.html with render (without json).

I can get the id values when I write {{ item.id }} but when I write {{ item.name }} I get the error *

“Query.Deferred exception: Madrid is not defined”

Note: project_list contains Barcelona(id=1) and Madrid(id=2) data.

Do you have any idea why that error occurs and how to get other info like name, address, email etc. ? Thank in advance.

views.py

project_list = Projects.objects.all().order_by('name').values('id', 'name')
..........
return render(request, 'base.html', {'project_list':project_list, 'form': form})

base.html

<script>
{% for item in project_list %}
   log.console({{ item.id }})
{% endfor %}
</script>

Please post the Projects model that you are trying to render. (We may end up needing to see the complete view and template, but probably not yet.)

Here is the Projects model. Email, address data is not in Projects model, but if I can make that work, I ll be able to apply all other models.

class Projects(models.Model):

    name = models.CharField(max_length=128, unique=True)
    prefix = models.CharField(max_length=256)
    description = models.CharField(max_length=256, blank=True)

    def __str__(self):
        return self.name

Just in case anybody interested in, I found the solution…

views.py
editableP = serializers.serialize("json", Projects.objects.all())

base.html
var eProject = JSON.parse('{{ editableP | safe }}')