Django REST framework (DRF) best practices (1)

Django REST framework (DRF) best practices (1)

I have summarized some reasonable configurations. If there are any shortcomings, you can give me some pointers. I am not just pointing out, but you must listen carefully.

settings, multi-environment configuration

Regarding settings, for many novices, they can just use the system settings to modify it. However, in practice, different environments will be distinguished, such as development environment and production environment, local or remote, etc. The suggestion is to create a folder called settings and create different configurations in the folder. Put some common ones in settings_base.py, such as some time zone settings, authentication settings, return formats, etc., and create a new development environment. dev.py, put some development tools into the development environment, such as debug_toolbar, some libraries used for testing during development. After creation, the directory is roughly as follows:

project_demo
├─apps
│ ├─app1
│ ├─app2
│ └─app3
├─project_demo
│ ├─settings
│ │ └─__init__.py
│ │ └─dev.py
│ │ └─prod.py
│ │ └─settins_base.py
│ ├─asgi.py
│ ├─urls.py
│ └─wsgi.py
└─utils
│ └─…
├─.env

Picture 1
Please note
that here you must pay attention to the __init__.py file in the settings folder. It cannot be without it and it needs to have content. For example, if I want to return the configuration of dev.py, then it must be in __init__.py write down:
figure 2

In dev.py, first import the public configuration, and then add or modify some additional configurations used for development, as shown in the picture: The
image 3
above picture is only my configuration, you can learn the structure, but don’t just copy it, you will get an error .

environment variables

Do not put it directly in settings, create a new .env file, and save the environment variables in .env.
For example:
.env
use as follows:

  1. Install python-dotenv
    pip install python-dotenv
    
  2. Used in settings
    # settings_base.py
    from dotenv import load_dotenv
    
    # 如果部署时得到的环境变量为空,可以在括号内加上文件 如load_dotenv(os.path.join(BASE_DIR, '.env'))
    load_dotenv()
    
    SECRET_KEY = os.environ.get("SECRET_KEY")
    

Use the apps folder to manage apps uniformly

In Django, the apps folder is usually used to manage applications uniformly. This makes the application easier to manage and maintain, especially as your projects become larger and more complex.

Here are the steps to use apps folder in DRF:

  1. Create a folder called "apps" in your project root directory.

  2. Create a new application in the "apps" folder. You can use the following command to create an application:

    python manage.py startapp myapp
    
  3. Add the application to INSTALLED_APPS. In the settings.py file, change the name of the application from "myapp" to "apps.myapp":

    INSTALLED_APPS = [
        ...
        'apps.myapp',
    ]
    

If an error occurs, change the name in the apps.py file in your application myapp to apps.myapp

The above is one method, the other is to configure and add the apps path in the settings. After the settings are completed, you don’t need to write apps. This part, but it will not be imported correctly in the editing tool, and the actual operation will not be displayed. The operation as follows:

#settings_base.py	
BASE_DIR = #省略

sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))

Follow RESTful design principles

pass

Serializers use

pass

Wait for further updates


Above, I record this and hope to help others who come after me. If it is helpful to you, I would be honored and thank you for your likes.

Guess you like

Origin blog.csdn.net/aifengaopeng/article/details/132830961