lundi 12 septembre 2022

Run a python function depends on the checkbox

I'm working on a python project and I want to call a function if a checkbox was checked. For expample if the yes checkbox was checked the first function will be called. I made this code, but it always calls the second function even if I checked the yes checkbox. I don't know what the issue. Any help is highly appreciated. this is my html :

<input type="checkbox" id="vehicle1" name="mycheckboxname" value="yes">
<label for="vehicle1"> Without comments</label><br>
<input type="checkbox" id="vehicle2" name="mycheckboxname" value="no">
<label for="vehicle2"> With comments</label><br>

And this is my view.py

def datatable_view(request):   
    if request.method =='POST':
        form = Scraping(request.POST)
        if form.is_valid():
            mylist=request.POST.getlist('mycheckboxname')
            if 'yes' in mylist: 
                subject=form.cleaned_data['subject'] 
                scrap(subject)
                client = pymongo.MongoClient("mongodb://localhost:27017/")
                db= client["db2"]
                col = db[subject]
                products = col.find()
                context = {'products' : products}
                return render(request,'datatable.html', context)
            else:
                subject=form.cleaned_data['subject']
                hello(subject)    
                client = pymongo.MongoClient("mongodb://localhost:27017/")
                # use variable names for db and collection reference
                db= client["db2"]
                col = db[subject]
                products = col.find()
                context = {'products' : products}
                #open datatable html and display all the data from database
                return render(request,'datatable.html', context)
    return



Aucun commentaire:

Enregistrer un commentaire