简体   繁体   English

Javascript与Django的集成

[英]Integration of Javascript with Django

I am rewriting an existing Java-based website in Python. 我正在用Python重写现有的基于Java的网站。 After much research, I've settled on using Django and have so far made some good progress with the project. 经过大量研究,我决定使用Django,到目前为止,该项目取得了一些良好的进展。

I am, however, stuck trying to use some of the codes from the previous website. 但是,我一直坚持尝试使用以前网站中的某些代码。 I am especially interested in using the scripts.js file which seems to have all the necessary JavaScript functions for the website, but not sure how to integrate it with my Django project. 我对使用scripts.js文件特别感兴趣,该文件似乎具有网站所有必需的JavaScript函数,但不确定如何将其与Django项目集成。

Would I be able to just simply put this file in my static folder and refer to the functions from individual apps? 我能简单地将此文件放在我的static文件夹中,并引用各个应用程序的功能吗? If so, how would I call the functions? 如果是这样,我将如何调用这些函数? Would I have to make any changes to the file given that it now has to work with a Python-based website, as opposed to a Java-based website? 鉴于文件现在必须与基于Python的网站(而不是基于Java的网站)一起使用,是否需要对文件进行任何更改?

I'm also attaching a snippet of the scripts.js file to give you an idea of what the functions look like. 我还附上了一段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);

Thanks! 谢谢!

You can put your javascript files in the static folder. 您可以将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 is a path relative to the static folder. path/to/your/file.js是相对于static文件夹的路径。

You should use django-compressor to minify your files on production environment. 您应该使用django-compressor来最小化生产环境中的文件。

As long as the routes you defined in the Django app are the same that in the Java app for the AJAX calls, you shouldn't have to change the content of your javascript file. 只要您在Django应用中定义的路由与在Java应用中为AJAX调用定义的路由相同,就不必更改javascript文件的内容。

Yes, you just need to put your script.js in your /static/ folder. 是的,您只需要将script.js放在/static/文件夹中。

static/
  js/
    script.js

In your templates you call it 在您的模板中将其称为

...
<script src={% static 'js/script.js' %}></script>
...

And that's all, you are able to use gatewayGetAllInformation and gatewayGetShifts functions. gatewayGetAllInformation ,您就可以使用gatewayGetAllInformationgatewayGetShifts函数。

But keep in mind update the constants values inside script.js 但是请记住更新script.js的常量值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM