0

this is my home page

I am working on a web application where user searches for a product and i am fetching that product from amazon flipkart and rendering on my home page.

All i want is to store the data in the database when the user clicks on "Get Alert" button for a specific product without refreshing the page

Here is my model class where data is supposed to be stored:

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)

Here is the view function that is been called when user searches for a product

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')

Once this view is called we get the above page showing all the products fetched from amazon and flipkart.

if the user clicks on "Get alert" button ,i want to store all the product info in the model

  • What you're looking for is AJAX. There is a great explanation on how to apply it in Django in this [answer from yuvi](https://stackoverflow.com/questions/20306981/how-do-i-integrate-ajax-with-django-applications). – vinkomlacic Apr 13 '22 at 08:43

0 Answers0