Django,如何从JS调用python?

时间:2017-05-16 11:03:33

标签: javascript python django function

我在一个名为Chatterbot 0.6的框架上工作。它是用Python编写的。

该项目基于网站和机器人。机器人在网站上实现,用户可以通过聊天框与他交谈。

直到这里,我在python和网站上做了聊天机器人。我们的任务是通过Django在网站上实现机器人。我总是设法实现网站,但现在,我必须使机器人工作。

聊天框用JS编写,机器人用Python编写。我编写网站的伙伴做了一个JS功能,在聊天框中提示机器人的答案。

我的问题是:如何在我的JS函数中实现Python?

var $msg_holder = undefined;
$(document).ready(function(){
    $msg_holder = $("#message_holder");
});

/**Function that appends a String (msg) to a DOM element ($dom)
*@param msg being the String to append (String object or literal string)
*@param $dom being a JQobject/JQselection of a DOM element to append the msg to
*/
function appendToDom(msg, $dom){
    $(msg).appendTo($dom);
}



/**Function that append a String (msg) to the Message Holder
*@param msg being the String to append (String object or string literal)
*/
function appendToMessageHolder(msg){
    appendToDom("<li>"+msg+"</li>", $("#message_holder"));
}

//////////////////////////////////////////////

/**Function that gets the amount of messages currently on the page
*@return the amount of messages currently on the page (unsigned integer)
*/
function processMsgCount(){
    var $ul = $("#message_holder");
    var $li = $ul.children("li");

    return Math.abs($li.length);//overkill to ensure that a positive number is returned
}

//////////////////////////////////////////////

/**Function that determines if it's the User's turn to give a sentence (in the scenario bot-->user user-->bot)
*@return TRUE if it's indeed the user's turn, FALSE otherwise
*/
function isUserTurn(){
    var ret = false;

    var MSGcount = processMsgCount();

    //if odd, then it's the User's turn
    if(MSGcount%2 != 0){
        ret = true
    }


    return ret;
}



/**Function that determines if it's the Bot's turn to give a sentence (in the scenario bot-->user user-->bot)
*@return TRUE if it's indeed the bot's turn, FALSE otherwise
*/
function isBotTurn(){
    return !isUserTurn();
}

//////////////////////////////////////////////

/**Function that displays the User's Input (wrapper function)
*@param msg being the user input itself (String object or literal string)
*/
function displayUserInput(msg){
    //condition is overkill, but meh 200% > 100%
    if(isUserTurn()){
        appendToMessageHolder(msg);
        scrollToLastMessage();//scroll down when new message
    }else{
//        console.log("user tried to input a sentence when it was the bot's turn :x");
        console.error("user tried to input a sentence when it was the bot's turn :x");
    }
}



/**Function that displays the Bot's Response to the User's Input (or display the initial sentence)
*@param msg being the bot's message/response (String object or literal string)
*/
function displayBotResponse(msg){
    //condition is overkill, but meh 200% > 100%
    if(isBotTurn()){
        appendToMessageHolder(msg);
        scrollToLastMessage();//scroll down when new message
    }else{
//        console.log("how the fuck did the bot try to give another message without user's input ?_?");
        console.error("how the fuck did the bot try to give another message without user's input ?_?");
    }

}

很抱歉标签使用不当 - '

非常感谢!

1 个答案:

答案 0 :(得分:0)

Python在服务器端运行,而你的js函数必须在客户端运行,如上所述。

根据我的信息,从js执行Python根本不可能。更重要的是,这对于您要实现的目标来说是不好的做法。

我们如何实现?

Ajax是你的良好起点我建议你去研究它。发出一个Ajax请求并制作你想要在某些事件上运行的机器人或任何Python片段。

希望这会对你有所帮助。