mardi 17 décembre 2019

store multiple check boxes in mysql from django form

I am not able to send the selected checkboxes value to mysql database. Please help me in finding out what i missed in the code. When i try to submit the form, it is just redirecting to the same form without any errors and in the same way it is not storing the values in database.

views.py

def personview(request):  
    if request.method == "POST": 
        form =PersonForm(request.POST)
        print(request.POST.getlist('choosetopics[]'))
        if form.is_valid():  
            try:
                form.save()


            except:  
                print("not valid")  
    else:  
        form = PersonForm()  

    return render(request,'person_list.html',{'form':form}) 


models.py

class Person(models.Model):
    name = models.ForeignKey(CollegeAttributes, on_delete=models.SET_NULL, null=True)
    domain = models.ForeignKey(Domains, on_delete=models.SET_NULL, null=True)
    choosetopics = models.ForeignKey(domain_topics, on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return self.name



Aucun commentaire:

Enregistrer un commentaire