【问题标题】:Model only responds if Windows Service is specified模型仅在指定 Windows 服务时响应
【发布时间】:2016-03-28 21:21:44
【问题描述】:

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

views.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

【问题讨论】:

  • 当你发布请求时service_name 有什么价值?
  • 服务名的值取自Page模型,var service_name = $("#{{ page.title }}").val();
  • 如果是这样就不会出现错误,那么当您在视图中设置断点时,service_name 会显示什么值?
  • 你的 js 代码具体在哪里?在模板中还是在不同的 js 文件中?在第一种情况下,模板到底在哪里?在这两种情况下,您期望像 "$("#{{ page.title }}")" 评估到什么?
  • 我现在检查正确,当它通过时,service_name 显示空白,而服务(这是另一个为启动/停止(不是来自模型)传递的变量)正在显示。任何线索为什么?跨度>

标签: javascript python ajax django python-2.7


【解决方案1】:

你想要的

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多