Как вызвать функцию представления в django без обновления страницы
Я работаю над веб-приложением, в котором пользователь ищет продукт, а я получаю этот продукт с amazon flipkart и отображаю на главной странице.
Все, что я хочу, это сохранить данные в базе данных, когда пользователь нажимает на кнопку "Get Alert" для определенного продукта без обновления страницы
Вот мой класс модели, где должны храниться данные:
class product(models.Model):
product_id=models.CharField(max_length=50,primary_key=True)
product_name=models.CharField(max_length=50)
link=models.URLField()
curr_price=models.IntegerField()
old_price=models.IntegerField()
website=models.CharField(max_length=10)
Вот функция представления, которая вызывается, когда пользователь ищет продукт
def search(request):
if request.method == "POST":
webList=request.POST.getlist('websites')
q=request.POST.get('search')
view=request.POST.get('view')
if(q=="" or len(webList)==0):
if request.user.is_authenticated:
return render(request,'userLogin/userDashboard.html',{'error':True,'search':q,'weblist':webList,'userName':request.user})
else:
return render(request,'home/searchProduct.html',{'error':True,'search':q,'weblist':webList})
AllWebProductList=[]
FlipkartList=[]
AmazonList=[]
shopcluesList=[]
snapdealList=[]
MyntraList=[]
if 'flipkart' in webList:
FlipkartList=getInfoFromFlipkart(q)
AllWebProductList.append(FlipkartList)
#return render(request,'home/searchProduct.html',{'lists':productObj})
if 'amazon' in webList:
AmazonList=getInfoFormAmazon(q) # Scrapping
# AmazonList=getInfoAmazon(q) #PAPI
AllWebProductList.append(AmazonList)
if 'shopclues' in webList:
shopcluesList=getInfoFromShopClues(q)
AllWebProductList.append(shopcluesList)
if 'snapdeal' in webList:
snapdealList=getInfoFromSnapDeal(q)
print ("welcome in snapdeal")
if 'ajio' in webList:
ajioList=getInfoFromAjio(q)
if 'myntra' in webList:
MyntraList=getInfoFromMyntra(q)
AllWebProductList.append(MyntraList)
print(" welcome in myntra")
#sorting(AllWebProductList)
mergeList=FlipkartList+AmazonList+shopcluesList
# sorting(mergeList,asc)
print(request.user.is_authenticated)
if request.user.is_authenticated :
return render(request,'userLogin/userDashboard.html',{'lists':mergeList,'val':view,'search':q,'weblist':webList,'userName':request.user})
else:
return render(request,'home/searchProduct.html',{'lists':mergeList,'val':view,'search':q,'weblist':webList})
#messages.warning(request,name)
return render(request,'home/searchProduct.html')
После вызова этого представления мы получаем приведенную выше страницу, показывающую все товары, полученные с amazon и flipkart.
если пользователь нажимает на кнопку "Get alert", я хочу сохранить всю информацию о продукте в модели