Home > database >  TypeError at /studentform Field 'prn_no' expected a number but got ('1',)
TypeError at /studentform Field 'prn_no' expected a number but got ('1',)

Time:10-31

I had a problem here....so I have created a MySql databases and connected it with Django framework, my website'titled 'School Management System' has 5 tables and all of them work flawlessly whether it is fetching data from user through Django forms and storing it in mysql database or vice versa but Student and faculty table returns same error as mentioned below :

TypeError at /studentform Field 'prn_no' expected a number but got ('1',).

I dont understand what is it that is leading to this error. Rest all

I tried deleting my whole Django migration and creating it again my python manage.py makemigration python manage.py migration

But still the same error.

Below are some snippets of my code:

studentform.html

{% extends 'base.html' %}

{% block title %}Student{% endblock title %}

{% block body %}

<div >
    <br>
    <form  method="post" action="/studentform">
    {% csrf_token %}

        <div >
            <label for="prn_no" >prn_no</label>
            <input type="number"  id="prn_no" name="prn_no" required>

        </div>

        <div >
            <label for="fname" >fname</label>
            <input type="text"  id="fname" name="fname" required>
        </div>

    
        <div >
            <label for="lname" >lname</label>
            <input type="text"  id="lname" name="lname" required>
        </div>
    
    <div >
            <label for="DOB" >DOB</label>
            <input type="date"  id="DOB" name="DOB" required>
        </div>

    <div >
            <label for="Address" >Address</label>
            <input type="text"  id="Address" name="Address" required>
        </div>

        <div >
            <label for="gender" >gender</label>
            <select  id="gender" name="gender" required>
                <option selected disabled value="">Choose...</option>
                <option>M</option>
                <option>F</option>
                <option>O</option>
            </select>
        </div>

    <div >
            <label for="Standard" >Standard</label>
            <input type="text"  id="Standard" name="Standard" required>
        </div>



      <div >
            <label for="guardian_id" >guardian_id</label>
            <input type="text"  id="guardian_id" name="guardian_id" required>
      </div>

        <br>
        <div >
            <button  type="submit" >Submit Data</button>
        </div>
    </form>
</div>

{% endblock body %}

views.py

def studentform(request):
    if request.method == "POST":
        prn_no = request.POST.get('prn_no'),
        fname = request.POST.get('fname'),
        lname = request.POST.get('lname'),
        dob = request.POST.get('dob'), 
        address = request.POST.get('address'), 
        gender = request.POST.get('gender'), 
        standard = request.POST.get('standard'),
        guardian_id = request.POST.get('guardian_id') 

        s1 = student1(prn_no = prn_no , fname = fname , 
                             lname = lname, dob = dob , address = address , 
                              gender = gender, standard = standard , guardian_id = guardian_id )

        s1.save()
    return render(request,'studentform.html')

models.py

class student1(models.Model):
    prn_no      = models.IntegerField()
    fname       = models.CharField(max_length=20)
    lname       = models.CharField(max_length=20)
    dob         = models.DateField()
    address     = models.CharField(max_length=20)
    gender      = models.CharField(max_length=20)
    standard    = models.CharField(max_length=20)
    guardian_id = models.CharField(max_length=20)

    class Meta:
        app_label = 'page1'
        db_table='student'

Traceback Error:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/studentform

Django Version: 4.0
Python Version: 3.11.0
Installed Applications:
['page1.apps.Page1Config',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\fields\__init__.py", line 1822, in get_prep_value
    return int(value)

The above exception (int() argument must be a string, a bytes-like object or a real number, not 'tuple') was the direct cause of the following exception:
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Aakash\Desktop\Mini Project\sms\page1\views.py", line 86, in studentform
    s1.save()
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\base.py", line 743, in save
    self.save_base(using=using, force_insert=force_insert,
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\base.py", line 780, in save_base
    updated = self._save_table(
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\base.py", line 885, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\base.py", line 923, in _do_insert
    return manager._insert(
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\query.py", line 1301, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\sql\compiler.py", line 1440, in execute_sql
    for sql, params in self.as_sql():
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\sql\compiler.py", line 1382, in as_sql
    value_rows = [
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\sql\compiler.py", line 1383, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\sql\compiler.py", line 1383, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\sql\compiler.py", line 1324, in prepare_value
    value = field.get_db_prep_save(value, connection=self.connection)
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\fields\__init__.py", line 839, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\fields\__init__.py", line 834, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\Aakash\anaconda3\envs\Django\Lib\site-packages\django\db\models\fields\__init__.py", line 1824, in get_prep_value
    raise e.__class__(

Exception Type: TypeError at /studentform
Exception Value: Field 'prn_no' expected a number but got ('1',).

CodePudding user response:

The error says prn_no is expected to be a string whereas a tuple is assigned to prn_no. To fix it, in your views.py file, you should remove commas at the end of each line.

def studentform(request):
    if request.method == "POST":
        prn_no = request.POST.get('prn_no')
        fname = request.POST.get('fname')
        lname = request.POST.get('lname')
        dob = request.POST.get('dob')
        address = request.POST.get('address')
        gender = request.POST.get('gender')
        standard = request.POST.get('standard')
        guardian_id = request.POST.get('guardian_id') 

        s1 = student1(prn_no = prn_no , fname = fname , 
                             lname = lname, dob = dob , address = address , 
                              gender = gender, standard = standard , guardian_id = guardian_id )

        s1.save()
    return render(request,'studentform.html')
  • Related