aquaman
aquaman

Reputation: 1658

cannot save modelform to database in django

hi i m new to django and made a biodata form but the problem is that when i try to save the form to the db it gives the error- (1146, "Table 'django_db.tictactoe_biodata' doesn't exist")

any help is appreciated..:)

my views.py

from django.shortcuts import render
from model import BiodataForm, Biodata

def get_name(request):
    if request.method == 'POST':
        post = request.POST
        form = BiodataForm(request.POST)
        if form.is_valid():
            biodata = form.save()
            firstname = post['first_name']
            lastname = post['last_name']
            return render(request, 'now.html', {'firstname': firstname, 'lastname': lastname})
    else:
        form = BiodataForm()
    return render(request, 'name.html', {'form': form})

my settings.py

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'django_db',
'PASSWORD': 'password',
'USER': 'djangouser',
'HOST': '',
'PORT': ''
}

}

and the errors-

>ProgrammingError at /getname/

>(1146, "Table 'django_db.tictactoe_biodata' doesn't exist")

>Request Method:    POST
>Request URL:   http://10.8.21.17:3369/getname/
>Django Version:    1.6.1
>Exception Type:    ProgrammingError
>Exception Value:   

>(1146, "Table 'django_db.tictactoe_biodata' doesn't exist")

>Exception Location:    /usr/lib/python2.7/dist-packages/MySQLdb/connections.py in         >defaulterrorhandler, line 36
>Python Executable:     /usr/bin/python
>Python Version:    2.7.6
>Python Path:   

>['/home/aquaman/tictactoe',
> '/usr/lib/python2.7',
 >'/usr/lib/python2.7/plat-x86_64-linux-gnu',
> '/usr/lib/python2.7/lib-tk',
> '/usr/lib/python2.7/lib-old',
> '/usr/lib/python2.7/lib-dynload',
> '/usr/local/lib/python2.7/dist-packages',
> '/usr/lib/python2.7/dist-packages',
> '/usr/lib/python2.7/dist-packages/PILcompat',
 >'/usr/lib/python2.7/dist-packages/gtk-2.0',
>'/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

>Server time:   Wed, 7 Jan 2015 10:25:26 +0000

i m using my college provided proxified net i dont know if the problem is ip address or anything else

thanks in advance

Upvotes: 0

Views: 157

Answers (2)

marco.santonocito
marco.santonocito

Reputation: 1703

You have to create migrations and run them. I see that you are using Django 1.6.1 so you have to follow these steps:

Open your settings.py and check if you have 'south' on your INSTALLED_APPS (If not, follow this guide)

Open the terminal

Go to the root of your project (where there is a file called manage.py)

Run the following commands:

python manage.py syncdb
python manage.py schemamigration nameofyourapp --initial
python manage.py migrate nameofyourapp
python manage.py schemamigration nameofyourapp --auto

Upvotes: 0

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

The error message seems quite clear: you have defined a model in django but forgot to create the model's table in your database. In django < 1.7, the builtin management command syncdb (https://docs.djangoproject.com/en/1.6/ref/django-admin/#syncdb) will create the table if it doesn't exist, but you'll have to manually take care of schema changes (if you add / remove / modify fields from an existing model), so I strongly advise you either use South (https://south.readthedocs.org/en/latest/) to handle schema and data migrations or just upgrade to django 1.7 which has builtin support for schema and data migrations.

As a side note: the good practice after a successfull post is to redirect (http://en.wikipedia.org/wiki/Post/Redirect/Get).

Upvotes: 1

Related Questions