tags:

views:

331

answers:

6

My views.py has become too big and it's hard to find the right view. So how do i split it in several files and then import it? Does it involve any speed loss? Can i do the same with models.py?

+4  A: 

Basically, you can put your code, whereever you wish. Just make sure, you change the import statements accordingly, e.g. for the views in the urls.py.

Not knowing your actual code its hard to suggest something meaningful. Maybe you can use some kind of filename prefix, e.g. views_helper.py, views_fancy.py, views_that_are_not_so_often_used.py or so ...

Another option would be to create a views directory with an __init__.py, where you import all subviews. If you have a need for a large number of files, you can create more nested subviews as your views grow ...

The MYYN
+6  A: 

In Django everything is python modules (*.py). You can create a view folder with an init.py inside and you still will be able to import it.. But an example would be better.

Your original views.py might look like this :

def view1(arg):
    pass

def view2(arg):
   pass

With the following folder/file structure it will work the same :

views/
   __init__.py
   viewsa.py
   viewsb.py

viewsa.py :

def view1(arg):
    pass

viewsb.py :

def view2(arg):
    pass

__init__.py :

from viewsa import view1
from viewsb import view2

The quick explanation would be : when you do from views import view1 i will look for view1 in

  1. views.py int the first case (original)

  2. in views/__init__.py in the second case. And __init__.py is able to provide view1 method because it imports it.

With this kind of solution, you might have no need to change import or urlpatterns arguments in urls.py

I actually don't know about speed issues (but I doubt there is).

For Models it might be a bit difficult.

Vincent Demeester
A: 

Since Django just expects a view to be a callable object, you can put then wherever you like in your PYTHONPATH. So you could for instance just make a new package myapp.views and put views into multiple modules there. You will naturally have to update your urls.py and other modules that reference these view callables.

You cannot do the same with models since they have a bit of automation around them which requires the models to be within an apps models module.

Horst Gutmann
A: 

Simple answer: Yes.

Best is to make a directory called views and then in your urls.py do:

import views
...
url(r'^classroom$', views.school.klass, name="classroom"),
Peter Bengtsson
+1  A: 

I've had to do this before (for clarities sake)

The way I did this was to create a views directory, then, in that, create a file called __init__.py

Now, when you're calling in your urls.py, you simply need to add another part

For example, previously, you may have called:-

url(r'^calendar/(?P<year>\d\d\d\d)/$', 'myproject.calendar.views.year')
url(r'^calendar/(?P<year>\d\d\d\d)/(?P<user>[a-z]+)/$', 'myproject.calendar.views.year_by_user')

You can now call something along the lines of

url(r'^calendar/(?P<year>\d\d\d\d)/$', 'myproject.calendar.views.year.index')
url(r'^calendar/(?P<year>\d\d\d\d)/(?P<user>[a-z]+)/$', 'myproject.calendar.views.year.user')

This is, of course, assuming that you had views/year.py containing the functions index and user ;)

Mez
A: 

I split almost all views in my apps into a views folder (with an init.py of course). I do not, however, import all of the subviews in the init.py like some of the answers have suggested. It seems to work just fine.

DrBloodmoney