TypeError at /form Field 'roof_age' expected a number but got ('1',)

Viewed 23

TypeError at /form Field 'roof_age' expected a number but got ('1',).

My model.py file

from django.db import models

# Create your models here.
class Details(models.Model):
    name = models.CharField(max_length=25)
    roof_age = models.IntegerField()
    email = models.EmailField()
    phone = models.IntegerField()
    address = models.TextField(max_length=100)
    monthly_bill = models.IntegerField()
    HOA = models.BooleanField(default=True)
    battery = models.BooleanField(default=True)
    foundation = models.BooleanField(default=True)
    roof_type = models.CharField(max_length=20)
    availability = models.DateTimeField()
    bill = models.FileField(upload_to='uploads/%d/%m/%y/')

    class Meta:
        verbose_name = 'details'
        verbose_name_plural = 'details'

    def __str__(self):
        return self.name 

My views.py file

def form(requests):
    if requests.method == 'POST':
        name=requests.POST['name'], 
        roof_age=requests.POST['roof_age'], 
        email = requests.POST['email'],
        phone = requests.POST['phone'], 
        address = requests.POST['address'],
        monthly_bill = requests.POST['monthly_bill'], 
        HOA = requests.POST['HOA'], 
        battery = requests.POST['battery'], 
        foundation = requests.POST['foundation'], 
        roof_type = requests.POST['roof_type'],
        availability= requests.POST['availability'],
        bill = requests.POST['bill']

        details = Details(
            name = name, 
            roof_age = roof_age, 
            email = email,
            phone = phone, 
            address = address,
            monthly_bill = monthly_bill, 
            HOA = HOA, 
            battery = battery, 
            foundation = foundation, 
            roof_type = roof_type,
            availability= availability,
            bill = bill
        )
        
        print(name, roof_age, email, phone, address, monthly_bill, HOA,battery, foundation, roof_type, availability, bill)
        print("The data has been save to db")
        details.save()

    return render(requests, 'baskin/form.html')

The print statement was giving the results in form of tuples

Now I'm getting TypeError: Field 'roof_age' expected a number but got ('1',).

How can I resovle this?

1 Answers

try to add int in this code: roof_age=int(requests.POST.get['roof_age']), maybe requests.POST get value type is string, but in your model.py roof_age is IntegerField.

Related