分类目录归档:Python-Dev

[ python人性化时间处理 ]


时间人性化(案例)

import datetime
import pendulum

# 把需要转换的时间字符串转为datetime对象,再转为字符串
now = datetime.datetime.strptime('2022-11-11 14:13:32', '%Y-%m-%d %H:%M:%S')
new_str = now.strftime('%Y-%m-%d %H:%M:%S')

# 把字符串转为pendulum时间对象
pt = pendulum.parse(new_str, tz='Asia/Shanghai')

< Read more >

[ DRF使用jwt ]


安装

pip install djangorestframework-simplejwt

配置

  • setting.py
INSTALLED_APPS = [
    ...
    'rest_framework_simplejwt',
    ...
]

REST_FRAMEWORK = {
  ...
  'DEFAULT_AUTHENTICATION_CLASSES': (
      ...
      'rest_framework_simplejwt.authentication.JWTAuthentication',
  )

< Read more >

[ django-drf序列化递归 ]


1.原生

  • 使用SerializerMethodField()
  • 定义函数def get_xxxx字段名(self,obj):
# 获取评论递归测试序列化器
class CommentSubsSerializers(serializers.ModelSerializer):
    user = UserInfoSerializers(read_only=True)
    ip_info = serializers.JSONField(source='get_ip_info', read_only=True)
    child = serializers.Serializ

< Read more >

[ Django源码分析 ]


VSCode配置

  1. 解释器
配置vscode python解释器
ctrl + shift + p
输入Python: select intxxxxxx
  1. 调试环境
把django源码文件复制到指定目录下

用vscode打开

创建调试器信息

创建调试器
ctrl + shift + d

创建成功后会在当前目录自动生成.vscode/launch.json文件

配置launch.json调试器配置
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descript

< Read more >

[ django中使用celery ]


安装依赖

  1. pip install celery
  2. pip install celery[librabbitmq]
  3. pip install django-celery-results

settings.py文件

CELERY_BROKER_URL = 'amqp://chf:chenhaifeng@47.120.55.99:5672' CELERY_TASK_TRACK_STARTED = True CELERY_TASK_TIME_LIMIT = 30 * 60 CELERY_BROKER_CONNECTION_RETRY_ON_STARTUP = True

主要配置

celery.py

i

< Read more >

[ cews项目 ]


local测试 dockerfile

解决下载慢问题

# define an alias for the specific python version used in this file.
FROM python:3.11.3-slim-bullseye as python

# Python build stage
FROM python as python-build-stage

ARG BUILD_ENVIRONMENT=local

RUN  sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.

< Read more >