Forums

sqlite3 database - table not found and forms not getting saved in db

Hi , I am new to Django framework.

I have created a python/django based app which is running on pythonanywhere, I recently added a new form to this app, this particular form is causing problems.

I get "no such table" error message on submitting the form. http://investaide.pythonanywhere.com/co_fins.html no such table: app_co_fin_model

I am using sqlite3 database, the on looking at the database, I was able to see the table corresponding to "app_co_fin_model".

Another issue , I have noticed is that by other forms are not getting saved in the db. http://investaide.pythonanywhere.com/ http://investaide.pythonanywhere.com/coffee-can.html

Are running fine and provide the desired output, however the form data is not visible in the database.

I am not sure if the problems are interrelated but it would be good to know your views.

Just to be clear, these same files and setup are working well in my local environment .. all of the form data is visible in the db.sqlite3 database.

extract from settings.py ....

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME' : 'db.sqlite3', } }

extract from models.py...

class co_fin_model(models.Model): exchange = models.CharField(max_length=200) company = models.CharField(max_length=200) sdate = models.DateField() edate = models.DateField()

extract from forms.py ...

class co_financials_form(forms.ModelForm):

exchange = forms.ChoiceField(initial= ia_stat.def_exchange,label='Exchange', choices=exchange)
company = forms.ChoiceField(initial= ia_stat.def_stock, label='Stock', choices=stocks)
sdate = forms.DateField(initial=sdate,label='Start Date', widget=forms.DateInput(attrs={'type': 'date'}))
edate = forms.DateField(initial=edate,label='End Date',widget=forms.DateInput(attrs={'type': 'date'}))


class Meta:
    model = co_fin_model
    fields = ["exchange", "company", "sdate", "edate"]

extract from views.py ...

@login_required(login_url="/login/") def cofin(request):

tbl_div_fin = "<div><h6 style=\"text-align:center; color:yellow;\"> </h6></div>"
tbl_div_fin2 = "<div><h6 style=\"text-align:center; color:yellow;\"> </h6></div>"

form = co_financials_form(request.POST or None)
message = "Please provide details"
msg2 = ""
msg_list = ""
print("%%%%", "Here1")
#html_template = loader.get_template( 'index.html' )
if form.is_valid():
    d1 = form.cleaned_data
    #print("######",d1)
    print("%%%%", "Here2coffee")
    exch = d1['exchange']
    comp = d1['company']
    sdate = d1['sdate']
    edate = d1['edate']

    form.save()
    message = "Get financials of  ..." + ia_sinfo.stock_name_code_map[comp][2] +" across market cycles"

Did you run your migrations on that database? See django-admin and manage.py | Django documentation | Django

Hi fjl, thanks. Yes, I have run migrations on the DB.

extract from bash ... (investaide.pythonanywhere.com) 12:03 ~/investaide.pythonanywhere.com (main)$ python manage.py makemigrations
&&&&& /home/InvestAide/investaide.pythonanywhere.com/media
No changes detected
(investaide.pythonanywhere.com) 12:03 ~/investaide.pythonanywhere.com (main)$ python manage.py migrate
&&&&& /home/InvestAide/investaide.pythonanywhere.com/media
Operations to perform:
Apply all migrations: admin, app, auth, contenttypes, sessions
Running migrations:
No migrations to apply.

Further .. tables sqlite3

(investaide.pythonanywhere.com) 12:03 ~/investaide.pythonanywhere.com (main)$ sqlite3 db.sqlite3
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
sqlite> .databases seq name file


0 main /home/InvestAide/investaide.pythonanywhere.com/db.sqlite3
sqlite> .tables
app_c_entry_model2 auth_user_groups
app_co_fin_model auth_user_user_permissions
app_coffee_can_model django_admin_log
auth_group django_content_type
auth_group_permissions django_migrations
auth_permission django_session
auth_user
sqlite> select * from 'app_co_fin_model' ...> ;
sqlite> select * from 'app_c_entry_model2';
sqlite>

as can be seen above, migrations was done, also tables exist in DB but are empty.

You're using a relative path here:

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME' : 'db.sqlite3', } }

If you do not pay attention too what your working directory is, then that could be referring to different files depending on how you are running your code. See https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/

Thank you , this is working now.