dividing up views.py into multiple files

166 views
Skip to first unread message

Stefan Foulis

unread,
Jan 9, 2007, 10:28:43 AM1/9/07
to django...@googlegroups.com
I searched around the django documentation and didn't find anything
about this...

what I'm trying to do is divide up the views.py file into multiple
files because the single views.py file in some of my apps is getting
very cluttered.

I tried just making a views subdirectory and then placing multiple
files with groups of views inside, referencing to them in urls.py
with an extra dot with the name...

classic reference:
ProjectName.AppName.views.functionName (using a single views.py)

then I tried this:
ProjectName.AppName.views.viewGroupFileName.functionName
(with a directory called "views" in the Application Directory and a
python File "viewGroupFileName.py" in there that has a function
called "functionName" inside)

With this I get a ViewDoesNotExitst: No module named
viewGroupFileName.functionName.

Is it possible to divide up the view into multiple files? Is this
also possible with models?

thanks for any hints

- stefan

Nathan R. Yergler

unread,
Jan 10, 2007, 9:54:40 AM1/10/07
to django...@googlegroups.com
Did you add a file named __init__.py to the views directory you created?
This file tells Python to treat the directory as a package and allow
traversal into the view file.

NRY

Rubic

unread,
Jan 10, 2007, 10:31:10 AM1/10/07
to Django users
Stefan Foulis wrote:
> what I'm trying to do is divide up the views.py file into multiple
> files because the single views.py file in some of my apps is getting
> very cluttered.
>
> I tried just making a views subdirectory and then placing multiple
> files with groups of views inside

Ditto to what Nathan suggested about putting an
empty __init__.py into your new views directory,
but even if you want to break up your views file
it's not necessary to create a seperate directory.
You could organize it something along the lines of:

mysite/
myapp/
views_eggs.py
views_ham.py

An alternative would be to keep everything that's
exposed to your templates in views.py, and the
secondary stuff in other file(s):

mysite/
myapp/
views.py
utils.py

Then in views.py, import the stuff from utils.py:

# views.py
from mysite.myapp.utils import supervalidator, xfabulator

No changes are necessary to urls.py, and you keep all
the 'public' functions in a single file.

P.S. Cum grano salis: My Django experience is less than
a week old.

--
Jeff Bauer
Rubicon, Inc.

Stefan Foulis

unread,
Jan 10, 2007, 12:25:42 PM1/10/07
to django...@googlegroups.com
thanks for the reply... adding the empty __init__.py file worked. I
like Rubics idea aswell. Importing extra utilities could prove very
flexible.

thanks again :)
-- stefan

Reply all
Reply to author
Forward
0 new messages