newbie programming here. I have a model with many lines, and I would like to pass each line to javascript.
First try:
Views.py
events = Events.objects.filter(user_id=user_id) // filter by user_id context = { "email": request.user.email, "login": True, "objects": events, } return render(request, 'fullcalendar/index.html', context)
Events is the name of the table, and I saved each row in Events . Passed this to a dict called context , which is then passed to my template. Then from my template, I was able to do something like this:
{% for object in objects %} <p>event.column_name</p> {% endfor %}
and this will work fine, however I cannot do this in the javascript section.
{% for object in objects %} var date = object.date
Second attempt
So, I did some research and decided to use json.
In Views.py, I made the following change:
return render(request, 'fullcalendar/index.html', {"obj_as_json": simplejson.dumps(context)})
and from this I was hoping to do this:
var objects = {{ obj_as_json }} for object in objects
But I got a QuerySet is not JSON Serializable Django error. So I looked at how to serialize objects and made the following change:
data = serializers.serialize('json', events.objects.all())
But I got the following error: 'QuerySet' object has no attribute 'objects'
Man, theres be an easier way to do what I want to do. Any ideas?