只有在指定了Windows服务时,模型才会响应
Model only responds if Windows Service is specified
在我的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 }}";
相关文章:
- 将 ExtJS 代理 AJAX 响应映射到模型
- JavaScript具有从响应生成的角度嵌套对象模型(无限深)
- 无法将存储的响应映射到模型 - EmberJS
- 使用Spine.Model.Ajax,如何在模型更新时处理响应的JSON中的额外属性
- 在单击事件时更新主干模型,并在更改位置之前等待响应
- 响应骨干关系中的类别模型更改
- jQuery ajax 响应绑定到 MVC 模型表中
- 煎茶触摸 2 商店 - 在创建模型之前修改响应
- 一个“;监听路由器”;(响应视图/模型中的路由器事件)
- 将静止响应转换为模型对象
- Ajax响应并没有改变我的视图模型
- 只有在指定了Windows服务时,模型才会响应
- 如何使用服务器响应更新Restangular模型对象
- 如何根据骨干模型的响应创建/更新骨干视图
- 如何在backbone.js回调中从模型中分离一些响应数据
- Backbone.js:如何处理模型和服务器响应之间的差异
- 我的主干集合中的响应/模型之间存在冲突
- 将服务器响应映射到骨干模型的可行方法
- 将响应值保存到进行Save()调用的骨干模型
- Backbonejs确定哪个函数从模型向视图返回了响应