Django application organization

I read the django tutorial, and it seems that all the viewing functions should be in a file called "views.py", and all the models are in the "models.py". I am afraid that there may be many view functions in my view.py file, and the same thing happens with models.py.

As far as I understand, django applications are correct?

Do Django applications allow us to share common functions across applications and maintain a minimum file size and file models? For example: My project may contain an application for recipes (create, update, view and search) and an application for friends, an application for comments, etc.

Can I transfer some of my view functions to another file? So, do I only have CRUD in one file?

+5
source share
4 answers

Firstly, large files are quite common in python. Python is not java that has one class for each file, not one module for each file.

Further, viewseven as the standard used, is a python module. A module does not have to be a single file. It could be a directory containing many files, and__init__.py

And then views.py- this is only an agreement. You, the application programmer, reference it, and django itself does not refer anywhere. Thus, you can put it in as many files and pass the appropriate functions to be transferred, the request inurls.py

+7
source

They do not need to go to views.py. They need to be referenced.

views.py . , , , , views.py.

models.py.

Django ? : (, , ) , .. .

"" - , - . , . , Django, .

+1

.

, , , , python:

views.py

views/__init__.py

- , def my_view(),

from views import my_view

!

, , django:

views/__init__.py
views/largemodel_view.py

__init__.py largemodel_view.py.

, , Meta.app_name:

class MyModel(models.Model):
    ...

    class Meta:
        app_name = 'yourappname'

django ( , Python!)

:

project/settings/__init__.py
                /..othersettings..
       /app_1/models/__init__.py
                    /...
             /views/__init__.py
                   /...
             /templates/
             /static/
             urls.py
       /urls.py

.

, , (URL- ..).

+1
0

All Articles