只有在指定了Windows服务时,模型才会响应

Model only responds if Windows Service is specified

本文关键字:模型 响应 服务 Windows      更新时间:2023-09-26

在我的views.py中,我通过ajax通过模板传递一个模型对象。它没有响应,但如果提到服务,它会响应。有人能告诉我为什么吗?

视图.py

def serv(request):
    if request.method == 'POST':
        if request.is_ajax():
            pythoncom.CoInitialize()
            service = request.POST.get('service')
            service_name = request.POST.get('service_name')
        # THIS "service_name" IS NOT GETTING RECOGNIZED BY WMI FUNCTION
            if service == 'stop':
                from socket import *
                c = wmi.WMI()
                for service in c.Win32_Service(Name=service_name):
                    result, = service.StopService()
                numb = 'Service stopped'
                data = {"status": numb}
                return JsonResponse(data)
            if service == 'start':
                from socket import *
                c = wmi.WMI()
                for service in c.Win32_Service(Name=service_name):
                    result, = service.StartService()
                numb = 'Service started'
                data = {"status": numb}
                return JsonResponse(data)
           pythoncom.CoUninitialize()
    return render(request,'django/index.html')

模板代码

{% if category %}
    {% if pages %}
        {% for page in pages %}
            <div class="form-group">
                <form method="POST">
                    {% csrf_token %} 
                    <h4>{{ page.title }}</h4>
                    <div class="col-xs-4">
                        <select class="form-control" id="{{ page.service }}" name="service">
                            <option id="start" value="start">start</option>
                            <option id="stop" value="stop">Stop</option>
                        </select>
                    </div>
                    <button class="btn btn-primary" id="{{ page.title }}" type="submit">Execute</button>
                    <span id="{{ page.result }}" class="label label-default"></span>
                </form>
            </div>
        {% endfor %}
        </ul>
    {% else %}
        <strong>No pages currently in category.</strong>
    {% endif %}
{% else %}
    The specified category {{ category_name }} does not exist!
{% endif %}
</br>
{% if category %}
     <a class="btn btn-default" href="/rango/category/{{ category_name_slug }}/add_page/">Add a New Service</a><br />
{% else %}
    A category by this name does not exist
{% endif %}

Javascript:

function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}
//For doing AJAX post
$("#{{ page.title }}").click(function(e) {
    e.preventDefault();
    var csrftoken = getCookie('csrftoken');
    var service = $("#{{ page.service }}").val();
    var service_name = $("#{{ page.title }}").val();
    $.ajax({
        url: '{{ page.url }}', // the endpoint,commonly same url
        type: "POST", // http method
        data: {
            csrfmiddlewaretoken: csrftoken,
            service: service,
            service_name: service_name
        }, // data sent with the post request
        // handle a successful response
        success: function(json) {
            console.log(json); // another sanity check
            //On success show the data posted to server as a message
            $("#{{ page.result }}").append( 'Response:' + json.status);
        },
        // handle a non-successful response
        error: function(xhr, errmsg, err) {
            console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
        }
    });
});

如果中特别提到了windows服务的名称(此处控制),则代码工作正常

for service in c.Win32_Service(Name="bthserv"): //Just an example of what works

您想要

 var service_name = $("#{{ page.title }}").get(0).id;

这实际上是一种非常复杂的写作方式:

var service_name = "{{ page.title }}";