How to load custom packages from inside Django apps into views.py?

Question!

I am new to Django and have a little problem with making all the project structure clear and organized and so on.. The MVC stuff in Django is quite easy to use when making a small project. However, when I am trying to get a new layer (application logic, like three-tier architecture) between views.py and models.py I have problem to do so.

I have the following structure:

  • mysite/
    • manage.py
    • app1/
      • models.py
      • views.py
      • logic/
        • __init__.py
        • Class1.py
        • parser.py
    • ...

and I want to load into views.py stuff from Class1.py and parser.py. How do I do it?

Neither of the following works:

from app1.logic import *

from app1.logic.Class1 import Class1

Also, it would help me if somebody could list me some example of a really huge django project. It looks like either lots of classes and .py files should be in every app folder or everything is in the models.py.. Both look a little disorganised and I'm sure there is a way to make it a little bit clearer (like in Zend or Symfony).

And, I'm working with Python3 and Django 1.5b2.

Thanks.



Answers

If Class1 or parser import from views, then you have a circular dependency. You'll need to move any shared code out into a third file.

You might consider though whether you need all those separate files under logic. In Python there's no requirement to have a class in its own file, so maybe you could have a single logic.py instead of a logic directory containing several files.



This video can help you solving your question :)
By: admin