从Django服务器触发javascript函数 [英] Trigger javascript function from Django server

查看:51
本文介绍了从Django服务器触发javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究IPN.每当我在以下网址中收到IPN: https://www.mywebsitename.com/notifications 时,我都想运行一个简单的JavaScript函数来显示一些html内容.

I am working on IPN's. Whenever I receive an IPN in this url: https://www.mywebsitename.com/notifications, I would like to run a simple JavaScript function that displays some html content.

我从服务器端管理IPN,如下所示:

I manage the IPN's from the server side, as follows:

@csrf_exempt
def notifications(request):
    if request.method == "POST":
       #some code

我想在该代码块中触发我的JS函数,但是我无法提出任何方法.另外,我真的不知道我要问的问题是否真的可能或不可能,也许还有我自己无法弄清楚的另一种方法.

I would like to trigger my JS function inside that block of code, but I can't come up with any way of doing this. Also I don't really know wether what I am asking is really possible or not, maybe there is another approach that I can't figure out by myself.

推荐答案

对于使用Websockets的Django,这是100%可行的.听起来您正在尝试基于在一个URL上收到请求的时间来为UI构建通知系统.首先检查Django频道: https://channels.readthedocs.io/en/stable/

This is 100% possible with Django using Websockets. It sounds like you are trying to build a notification system for the UI based on when you receive a request at one of your urls. Start by checking out Django Channels: https://channels.readthedocs.io/en/stable/

这是在Django中使用Websockets的最佳方法,它可以帮助您实现通知或其他实时功能.文档中的教程很棒.要解决您的任务,您应该:

This is the best way to use Websockets with Django, which can help you implement notifications or other real-time features. The tutorial in the docs is great. To solve your task, you should:

  • 按照该教程进行操作,并在Django应用中设置WebSocket使用者以处理通知.这样一来,前端用户就可以与您的应用程序建立实时连接并从中接收消息.
  • 完成您的notifications_view.付款请求发出后,您将向用户需要接收消息的Websocket使用者发送消息.它最终可能看起来像这样:
# assuming that the request gives you the username of the user to be notified,
# and you have a group configured in your consumer

def notification_view(request):
     if request.method == "POST":
          username = request.POST.get('username')
          group_name = 'notifications{}'.format(username)
          channel_layer = channels.layers.get_channel_layer()
          async_to_sync(channel_layer.group_send)(
                 group_name,
                 {
                     'type': 'new_payment',
                     'text': {
                         'eventType': 'notification',
                         'name': 'New Payment',
                         'userMessage': 'You just got paid'
                     }
                 }
     return(HttpResponse(status=200))

这将在收到请求时通过用户的套接字发送一条消息.

This would send a message over the user's socket when the request is received.

在您的js中,您将为套接字设置一个侦听器.然后,您可以侦听消息,并使用接收到的数据在文档中做任何您想做的事情,例如向用户显示消息:

In your js, you will setup a listener for the socket. Then you can listen for the messages and do whatever you desire in the document with the data you recieve, such as show a user a message:

var endpoint = 'ws://' + '<yourhost>/notifications/username/;
var socket = new ReconnectingWebSocket(endpoint);

socket.onmessage = e => {
     data = JSON.parse(e.data);
     let msg = document.createElement('<p>');
     msg.innerText = data['text']['userMessage'] 
    }

只需按照教程进行操作,那肯定会使您朝着正确的方向前进!

Just follow the tutorial and that will certainly get you headed in the right direction!

这篇关于从Django服务器触发javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆