Hello folks,
I am new to both Python and Django. And the story is a little long. So, please bear with me.
Recently I created a site named djangoSite to practice the Django framework. The first thing I want to do is to create a SQLite database to hold global data of the site. As simple as it sounds, the actual process I went through wasn't so simple.
First, I created an apps/ subdirectory under djangoSite/ to hold all potential applications I may have. Then, I created an empty file __init__.py to make apps/ a package.Then, I wanted to create an app global to make models for holding global data. I tried the following to do this.
djangoSite/ >$ cd apps
apps/ >$ ../manage.py startapp global
It turns out the app global was created under djangoSite/ rather than under apps/. So, here is one question. How do I create a sub-app under apps/ with manage.py? Anyway, I didn't stop there. I moved the folder global/ into apps/. I assumed that this would make global a subapp of apps, so that something like this would work under manage.py shell: import apps.global. However, I didn't test it right after moving the folder global/, which I should have. Instead, I proceeded and created a model in apps/global/models.py named NavItem. Then, I added 'djangoSite.apps.global' to settings.INSTALLED_APPS.
Then, I added a SQLite3 database 'global' in settings.DATABASES, and created a Python file dbrouter.py under djangoSite/, in which I created a class DBRouter to route the databases. Accordingly, I added
DATABASE_ROUTERS = ['djangoSite.dbrouter.DBRouter'] to settings.py. Here is another question. When creating the class DBRouter, I basically copied the example (only the master class) in the Django documentation
Multiple Databases. It seems to me the newly created routing class, which is DBRouter in my case, inherits a class named object, because of this statement: class DBRouter(object). However, the example in the document I mentioned earlier doesn't import anything at all, so the question is
where this class 'object' come from?
Anyway, I kept moving. Under djangoSite/, I ran ./manage.py validate, which gave 0 error. Then, I ran ./manage.py syncdb. And I got a bunch of errors. I suspected that the database routing may not work. So, commented out the DATABASE_ROUTERS settings, and ran ./manage.py syncdb again. This time, it went through without errors. Now I am guessing that I need to import something to make DBRouter work.
So I started tracking down the problem. I invoked manage.py shell, and wanted to create an instance of NavItem. I tried the following import statements, but none of these worked. All of the them get a syntax error. Here thery are.
>>> from apps.global.models import NavItem
File "<console>", line 1
from apps.global.models import NavItem
^
SyntaxError: invalid syntax
>>> from global.models import NavItem
File "<console>", line 1
from global.models import NavItem
^
SyntaxError: invalid syntax
>>> from djangoSite.apps.global.models import NavItem
File "<console>", line 1
from djangoSite.apps.global.models import NavItem
^
SyntaxError: invalid syntax
>>> from global.models import NavItem
File "<console>", line 1
from global.models import NavItem
^
SyntaxError: invalid syntax
Now I am running out of ideas about what went wrong. I know this is a bit long, so I really appreciate your help. Thank you very much.
Jim
, I also created and an subdirectory apps to hold all my django applications. I created an empty file __init__.py to make the folder apps a package. Then, I meant to create an app named global to hold global data of the site.