Как размещать несколько строк в django с помощью ajax
как я могу сохранить несколько строк при нажатии на кнопку сохранения. это фреймворк django. Я представил свою модель и файл шаблона. но пожалуйста, помогите мне найти, как сохранить несколько строк при нажатии на кнопку сохранения
это мой класс модели
class Items(models.Model):
item_name=models.CharField(max_length=50)
item_code=models.CharField(max_length=50)
desc=models.CharField(max_length=50)
price=models.IntegerField()
это мой шаблонный класс
я хочу сохранить все строки таблицы, нажав на кнопку сохранить как мне написать мой view.py и ajax код
вы можете написать такую функцию
def index(request):
if request.method == "POST":
name = request.POST.get('item_name')
...
Items.objects.create(
item_name=name,
...
)
return render(request, 'index.html')
return render(request, 'user/index.html')
my Views.py
@csrf_exempt
def add_bills(request):
title = "Jayalath Enterprises"
header = "Jayalath Enterprises Home Page"
context = {
"title": title,
"header": header
}
if request.method=="POST":
item_name=request.POST.get('item_name')
item_code=request.POST.get('item_code')
item_desc=request.POST.get('item_desc')
item_price=request.POST.get('item_price')
saverec=Items()
saverec.item_name=item_name
saverec.item_code=item_code
saverec.desc=item_desc
saverec.price=item_price
saverec.save()
messages.info(request,'success')
return render(request,"Addbills.html")
else:
messages.info(request,'fail')
return render(request, "Addbills.html",context)
вот мой AJAX код, согласно моему коду он POST и сохраняет эти значения в моей базе данных | item_name | item_code |desc |price | --------- | ----------|-----|----- | xxxx | ssss |sssss|25
но я хочу сохранять все строки одновременно, когда пользователь вводит данные. пожалуйста, помогите мне
$('#save').click(function(){
var item_name=[];
var item_code=[];
var item_desc=[];
var item_price=[];
<!-- $('.item_name').each(function(){-->
<!-- item_name.push($(this).text());-->
<!-- });-->
<!-- $('.item_code').each(function(){-->
<!-- item_code.push($(this).text());-->
<!-- });-->
<!-- $('.item_desc').each(function(){-->
<!-- item_desc.push($(this).text());-->
<!-- });-->
<!-- $('.item_price').each(function(){-->
<!-- item_price.push($(this).text());-->
<!-- });-->
$.ajax({
url:"",
method:"POST",
data:{item_name:'xxxx',item_code:'ssss',item_desc:'sssss',item_price:25},
success:function(data){
console.log(data);
}
});