如何使用从python到javascript的数组?姜戈
How to use array from python to javascript? Django
在你说它重复之前!!
我知道如何在视图中使用 dump() 进行解析,但现在我想从自定义模板标签接收字典数组 JavaScript.
这是我的 custom_template.py
@register.simple_tag(takes_context = True)
def messageRequest(context):
request = context['request']
messageData = Message.objects.all()
jsMessageData = []
if 'user' in request.COOKIES:
currentUser = request.COOKIES.get('user')
i = 0
for i in range(messageData):
if messageData[i].receiver == currentUser:
jsMessageData.append(messageData[i])
if messageData[i].sender == currentUser:
jsMessageData.append(messageData[i])
i += 1
print(jsMessageData)
return dumps(jsMessageData)
对于我的模板 index.html 我想在 JavaScript 中使用它来显示消息-
我试过这个
messageData = Json.parse("{% messageRequest %}");
// i want to store it as array
// throws error "'QuerySet' object cannot be interpreted as an integer" when looping through it
另外,
messageData = Json.parse("{% messageRequest|escapejs %}");
// i read this somewhere and it works fine when array is passed through render request but here it gives error as it considers escapejs as part of block tag.
感谢任何帮助,我是初学者所以请尝试给出全面的解释,一个例子将非常有帮助。
编辑:这是我的模型-
class Message(models.Model):
sender = models.CharField(max_length=100)
receiver = models.CharField(max_length=100)
message = models.CharField(max_length=100)
time = models.CharField(max_length=100, null=True)
def __str__(self):
return (self.sender + '-' + self.receiver)
我在一些事情上取得了进步你可以检查问题here
@register.simple_tag(takes_context = True)
def messageRequest(context):
request = context['request']
if 'user' in request.COOKIES:
currentUser = request.COOKIES.get('user')
messageData = [Message.objects.filter(sender=currentUser).values()]
return messageData
也可以使用@login_required
装饰器只允许登录用户访问视图,然后就可以访问current_user = request.user
在你的模板中你应该添加
message_data = {{ message_data | safe }}
您必须在视图的上下文中传递 message_data
才能在模板中访问它 os。
您可以使用原版 javascript 或 Jquery.
执行过滤器等操作
在你说它重复之前!! 我知道如何在视图中使用 dump() 进行解析,但现在我想从自定义模板标签接收字典数组 JavaScript.
这是我的 custom_template.py
@register.simple_tag(takes_context = True)
def messageRequest(context):
request = context['request']
messageData = Message.objects.all()
jsMessageData = []
if 'user' in request.COOKIES:
currentUser = request.COOKIES.get('user')
i = 0
for i in range(messageData):
if messageData[i].receiver == currentUser:
jsMessageData.append(messageData[i])
if messageData[i].sender == currentUser:
jsMessageData.append(messageData[i])
i += 1
print(jsMessageData)
return dumps(jsMessageData)
对于我的模板 index.html 我想在 JavaScript 中使用它来显示消息- 我试过这个
messageData = Json.parse("{% messageRequest %}");
// i want to store it as array
// throws error "'QuerySet' object cannot be interpreted as an integer" when looping through it
另外,
messageData = Json.parse("{% messageRequest|escapejs %}");
// i read this somewhere and it works fine when array is passed through render request but here it gives error as it considers escapejs as part of block tag.
感谢任何帮助,我是初学者所以请尝试给出全面的解释,一个例子将非常有帮助。
编辑:这是我的模型-
class Message(models.Model):
sender = models.CharField(max_length=100)
receiver = models.CharField(max_length=100)
message = models.CharField(max_length=100)
time = models.CharField(max_length=100, null=True)
def __str__(self):
return (self.sender + '-' + self.receiver)
我在一些事情上取得了进步你可以检查问题here
@register.simple_tag(takes_context = True)
def messageRequest(context):
request = context['request']
if 'user' in request.COOKIES:
currentUser = request.COOKIES.get('user')
messageData = [Message.objects.filter(sender=currentUser).values()]
return messageData
也可以使用@login_required
装饰器只允许登录用户访问视图,然后就可以访问current_user = request.user
在你的模板中你应该添加
message_data = {{ message_data | safe }}
您必须在视图的上下文中传递 message_data
才能在模板中访问它 os。
您可以使用原版 javascript 或 Jquery.
执行过滤器等操作