Import problem

25 views
Skip to first unread message

Jim

unread,
Aug 21, 2011, 2:49:32 AM8/21/11
to django...@googlegroups.com
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. 

Cal Leeming [Simplicity Media Ltd]

unread,
Aug 21, 2011, 9:40:02 AM8/21/11
to django...@googlegroups.com

I could be wrong but ,i dont think the word 'global' is allowed anywhere in a file name ,import reference, class name etc.. i could be wrong tho. Try with global2 ??

On Aug 21, 2011 2:10 PM, "Jim" <jianb...@gmail.com> wrote:
> 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__.pyto makeapps/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<https://docs.djangoproject.com/en/dev/topics/db/multi-db/#topics-db-multi-db-routing>.
> 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*?
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/3cTQeJLV05gJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

Jim

unread,
Aug 21, 2011, 10:54:05 PM8/21/11
to django...@googlegroups.com
You are right, SleepyCal. I changed global to something else, and the import issue was solved.
Reply all
Reply to author
Forward
0 new messages