随笔分类 - Django小知识
摘要:1. 使用 from cryptography.fernet import Fernet 第三方库 pip3 install cryptography 2. Fernet的使用 from cryptography.fernet import Fernet # 生成加密密钥 key = Fernet.
阅读全文
摘要:1. Django3.2版本升级后的一些报错, 导致项目运行失败 1.1 site-packages/rest_framework/serializers.py from django.db.models.fields import FieldDoesNotExist ImportError: ca
阅读全文
摘要:1. orm order_by 对字段进行排序, 按中文 models.EnterpriseInfo.objects.extra(select={'name_pinyin': "CONVERT(name USING GBK)"}).order_by('name_pinyin').values('id
阅读全文
摘要:#### 1. django-simple-captcha 下载 `pip install django-simple-captcha` #### 2. 配置 ##### 2.1 settings.py 配置 ```python # 注册 app INSTALLED_APPS = [ .... 'c
阅读全文
摘要:#### admin.py 后台管理界面修改密码 ```python from django.contrib import admin from django.contrib.auth.hashers import make_password from django.http import Http
阅读全文
摘要:```python from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger def main(object_list, page_index, display_num=10): """ :param objec
阅读全文
摘要:#### 1. GET请求 ```python # query请求 def get(self, request): print(request.GET) res = [] # 最终返回的结果集合 search_field = request.GET.get('search_field', '') p
阅读全文
摘要:自定义分页类 # from rest_framework.pagination import PageNumberPagination from rest_framework.response import Response class MyPagination(PageNumberPaginati
阅读全文
摘要:获取数据表的字段 from core import models temp_val = models.FactorInfo._meta.get_fields() field_list = [i.name for i in temp_val] # ['enterpriseinfo', 'siteinf
阅读全文
摘要:1. mysql数据库 models.TerminalInfo.objects.filter(status=True).extra( select={'created_at': 'date_format(created_at, "%%Y-%%m-%%d %%H:%%i:%%s")'})[ page_
阅读全文
摘要:api.py from dss.Serializer import serializer from django.http import HttpResponse def response_as_json(data, foreign_penetrate=False): jsonString = se
阅读全文
摘要:Windowds 运行 安装pip install eventlet celery -A AtmosphericTransportPlatform worker -c 10 -l info -P eventlet -Q reconstruct_task_compute AtmosphericTran
阅读全文
摘要:mysqlclient 1.4.0 or newer is required 1. 报错原因: MySQLclient 目前只支持到 Python3.4,而本人使用了更高版本的 python(Python3.7) 2. 解决方法: import pymysql pymysql.version_inf
阅读全文
摘要:python 'from docx import Document'报错 ModuleNotFoundError: No module named ‘exceptions 因为 python3移除 exceptions模块后造成的, 需要将已经安装的 docx库删除, 然后再 'pip instal
阅读全文
摘要:1. 导出依赖包 pip freeze > requirements.txt 2. 安装依赖包 pip install -r requirements.txt
阅读全文
摘要:1. 默认反向查询 小写表明_set # 查询 计算机科学与技术2班的所有学生的信息 models.Clas.objects.get(name="计算机科学与技术2班").student_set.all() 2. 按照自定义的别名查询 clas = models.ForeignKey('Clas',
阅读全文
摘要:values_list('name', flat=True) 获取某个字段的值列表 # 只适合获取一个值的时候 name_list = models.RadarStaid.objects.values_list('name', flat=True) # queryset对象, 包含name值列表 #
阅读全文
摘要:1. get_host() 获取ip地址 request.get_host() 2. get_port() 获取端口号 request.get_port() 3. build_absolute_uri() 获取 完整ip及端口 # build_absolute_uri() 参数里面字符串 带有'/'
阅读全文
摘要:1. settings.py STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'statics') MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'medi
阅读全文
摘要:1. settings 配置 MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 2.1 urls 配置(方式一) from django.conf.urls.static import static from dja
阅读全文