Javascript与Djang的集成

2024-09-29 19:17:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python重写一个现有的基于Java的网站。经过大量的研究,我决定使用Django,并且到目前为止,这个项目已经取得了一些良好的进展。在

然而,我却无法使用之前网站上的一些代码。我对使用scripts.js文件特别感兴趣,它似乎具有网站所需的所有JavaScript功能,但不确定如何将其与我的Django项目集成。在

我能简单地把这个文件放在我的static文件夹中并引用各个应用程序的函数吗?如果是,我该如何调用函数?鉴于文件现在必须使用基于Python的网站,而不是基于Java的网站,我是否需要对该文件进行任何更改?在

我还附加了一个scripts.js文件的片段,让您了解函数的样子。在

function gatewayGetAllInformation(){
    if(controlsLocked) {
        return;
    }
    lockControls();
    getFiles();
    gatewayGetShifts();
    getCatNews();
    getDepNews();
    getSales();
    getPermanentSales();
    adminGetUsers();
    unlockControls();
};

function gatewayGetShifts(){
    var url = root +"/AjaxResponder?action=getShifts";
    var xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystate = function() {
        if(xmlHttp.readyState == 4) {
            if(xmlHttp.status == 200) {
                var shiftsHTML =gatewayParseShifts(xmlHttp.responseXML);
                setInnerHTML("upcomingShiftsList",shiftsHTML); // added semicolon
            }
            else {
                setInnerHTML("upcomingShiftsList", "Error Getting Users");
            }
        }
    };
    xmlHttp.open("POST", url, true);
    xmlHttp.send(null);

谢谢!在


Tags: 文件项目django函数urlif网站var
2条回答

是的,您只需要将您的script.js放在/static/文件夹中。在

static/
  js/
    script.js

在你的模板中你称之为

^{pr2}$

仅此而已,您可以使用gatewayGetAllInformation和{}函数。在

但请记住更新script.js中的常量值

您可以将javascript文件放在静态文件夹中。在

<!  on top of the template file  >
{% load static %}

<!  Where you want to load the javascript file template file  >
<script type="text/javascript" src="{% static 'path/to/your/file.js' %}"></script>

path/to/your/file.js是相对于static文件夹的路径。在

您应该使用django-compressor缩小生产环境中的文件。在

只要在Django应用程序中定义的路由与Java应用程序中为AJAX调用定义的路由相同,就不必更改javascript文件的内容。在

相关问题 更多 >

    热门问题