Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django app organization

Tags:

python

django

I have been reading some django tutorial and it seems like all the view functions have to go in a file called "views.py" and all the models go in "models.py". I fear that I might end up with a lot of view functions in my view.py file and the same is the case with models.py.

Is my understanding of django apps correct?

Django apps lets us separate common functionality into different apps and keep the file size of views and models to a minimum? For example: My project can contain an app for recipes (create, update, view, and search) and a friend app, the comments app, and so on.

Can I still move some of my view functions to a different file? So I only have the CRUD in one single file?

like image 577
iJK Avatar asked Jun 07 '10 01:06

iJK


2 Answers

First, large files are pretty common in python. Python is not java, which has one class per file, rather one module per file.

Next, views, even as the standard used, is a python module. A module need not be a single file. It can be a directory containing many files, and __init__.py

And then, views.py is only a convention. You, the application programmer are referring to it, and django itself doesn't refer anywhere. So, you are free to put it in as many files and refer appropriate functions to be handed over, the request to, in the urls.py

like image 190
lprsd Avatar answered Sep 27 '22 22:09

lprsd


They don't have to go in views.py. They have to be referenced there.

views.py can include other files. So, if you feel the need, you can create other files in one app that contain your view functions and just include them in views.py.

The same applies to models.py.

Django apps lets us separate common functionality into different apps and keep the file size of views and models to a minimum? For example: My project can contain an app for recipes (create, update, view, and search) and a friend app, the comments app, and so on.

I don't know about the "to a minimum" part - some apps are just big in views, others big in models. You should strive to partition things well, but sometimes there is just a lot of code. But other than that, this is a fair summary of Django apps, yes.

like image 24
cethegeek Avatar answered Sep 27 '22 23:09

cethegeek