Separating Django App Views

前端 未结 4 1132
一向
一向 2021-02-04 06:17

If the app/views.py file gets very large, should I separate it? If so, what is the best way to do this?

4条回答
  •  生来不讨喜
    2021-02-04 06:44

    There is no generic best way. But there is a right way for your situation.

    • put the views in their own files and import them in your view. This is good just to see how it works
    • make a separate app inside the project to maintain a set of views
    • create your own generic views which share the views common to most of your apps

    Just as an starting example: I recommend you to start from the model and work yourself up:

    • how many models do you have?
    • Do they actually all relate or can they be grouped?
    • if the can be grouped split the app into two apps
    • so you'll also split the views
    • determine which view functions are similar and make them generic.

提交回复
热议问题