Есть определенная кнопка в list.html:
{% load static %}
...
{% include "execute.html" %}
...
<button type="button" id="id">Execute Script</button>
views.py:
import subprocess
import os
...
def exec_com(request):
if request.is_ajax():
if request.method == 'POST':
command = request.POST['cmd']
os.chdir('../static/tools/')
return subprocess.call(command, shell=True)
return HttpResponseRedirect(reverse('list'))
urls.py:
url(r'^execute/$', views.exec_com, name='execute'),
execute.html:
{% load static %}
<script src="{% static 'js/jquery.cookie.min.js' %}"></script>
<script type="text/javascript">
$(document).on('ready', function(){
$('#id').on('click', function(){
data = {
"cmd": "id.py"
}
$.ajax({
headers: { "X-CSRFToken": $.cookie("csrftoken") },
url : window.location.href,
type: "POST",
data: data,
dataType: "text",
async: "asynchronous",
success: function(data) {
alert("Executed.")
console.log(data)
},
error: function(xhr,errmsg,err) {
alert("Something wrong.")
console.log(xhr.status + ": " + xhr.responseText)
}
});
});
});
</script>
При нажатии выполнения скрипта не происходит. Подскажите, пожалуйста, что я пропустил или не так сделал?
Заранее спасибо.
Вам нужно написать функцию на python, которая будет ожидать запрос с какими-то параметрами или без них по указаному адресу (url) и обращаться к ней. Статика тут не причем, она обрабатывается браузером, python-скрипт обрабатывается интерпретаторм.
urls.py:
url(r'^url_name/$', views.get_for_url_name)
Функция, которая обработает запрос
views.py:
# -*- coding: utf-8 -*-
from django.http import JsonResponse
def get_for_url_name(request):
if request.method == 'GET':
data = ' method GET'
elif request.method == 'POST':
data = ' method POST'
else:
data = ''
a = {'Hello world': data}
response = JsonResponse(data)
response['Access-Control-Allow-Origin'] = '*'
return response
Теперь можно обратиться к этой функции из браузера http://localhost:port/get_for_url_name или написать соответствующий запрос на JS
Должно работать :)
Как вести несколько рекламных аккаунтов в антидетект-браузере
Подготовка и масштабирование рекламных кампаний в среде Linken Sphere