Код jQuery выглядит следующим образом:
Код: Выделить всё
$(document).on('click','#exModel',function () {
const sending = [];
$("table tr").each(function () {
var p1 = $(this).find("th label").html();
var p2 = $(this).find("td input").attr('id');
var p3 = $(this).find("td input").val();
const build = [];
build.push(p1, p2, p3);
sending.push(build);
console.log(sending);
});
$.ajax({
url: '../coreqc/exModel/',
data: {'sending': sending},
type: 'post',
headers: {'X-CSRFToken': '{{ csrf_token }}'},
async: 'true',
success: function (data) {
console.log("I made it back")
//dom: 'Bfrtip',
}
});
});
Код: Выделить всё
[Log] [["Product A", "1", ""], ["Product B", "2", ""], ["Product C", "3", ""], ["Product D", "4", ""], ["Product E:", "5", ""], ["Product F", "6", ""], ["Product G", "7", ""], ["Product H", "8", ""], ["Product I", "9", ""], ["Product K", "10", ""], …] (36) (coreqc, line 491)
[Log] I made it back # This is the success text in the above jQuery code
представление exModel:
Код: Выделить всё
def exModel(request):
sentData = request.POST
print(sentData)
template = loader.get_template('coreqc/tester.html')
context = {
'sentData':sentData
}
return HttpResponse(template.render(context, request))
Код: Выделить всё
sending[1][]Код: Выделить всё
get.values() — работает, и я могу печатать на консоли — выглядит так же, как указано выше. p>
Я могу зациклить и распечатать вот так:
Код: Выделить всё
for x, obj in sentData.items():
print(x)
for y in obj:
print(y + ':', obj[y])
Код: Выделить всё
sending[0][]
sending[1][]
sending[2][]
sending[3][]
Итак, подведем итог:
- Правильно ли я отправляю данные из jQuery? правильно — это способ, с которым Python Django может справиться.
- Как мне зациклить указанные данные, чтобы получить доступ к каждому полю данных.
Подробнее здесь: https://stackoverflow.com/questions/791 ... jango-view
Мобильная версия