Django的问题来这里看看看吧
1、django在centos7中报sqlite3版本错误解决
# 安装相关包
pip3 install pysqlite3
pip3 install pysqlite3-binary
修改django库相应代码
# 找到自己安装的路径
vi /usr/local/python3/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py
# 修改内容
# from sqlite3 import dbapi2 as Database
from pysqlite3 import dbapi2 as Database
2、django 修改后台的名称及应用名称
后台顶部标题的修改
app/admin.py中设置
admin.site.site_title = "笔记管理系统"
admin.site.site_header = "笔记管理后台"
应用的名称修改
第一步:修改apps.py文件,在name所在行下方增加
verbose_name = "学习笔记"
第二步:修改__init__.py文件,添加以下内容:
default_app_config = "note_wx.apps.NoteWxConfig" # 自改
表名称的修改
在models.py 对应的类中
class Meta:
verbose_name = '内容'
verbose_name_plural = '内容'
字段名称的修改
则在定义中增加参数 verbose_name='中文'
ctitle = models.CharField(max_length=20,verbose_name='分类名称')
3、Django–Restframework–coreapi版文档
安装包
pip install djangorestframework
pip install markdown # Markdown support for the browsable API.
pip install django-filter
pip install Pygments
pip install coreapi
pip install PyYAML
项目url下设置文档路由
from rest_framework.documentation import include_docs_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('docs/', include_docs_urls(title='My API Title')),
]
项目settings
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
# 'DEFAULT_PERMISSION_CLASSES': [
# 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' # 适用于添加身份验证和权限以后。
# ]
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema' # (推荐) 因为新版的restframework需要指定默认schema
# 或者 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema'
}
BUG:AssertionError: coreapi
must be installed for schema support.
解决办法:主要问题是urllib3的版本,降低版本
pip install urllib3==1.26.15
4、Django–drf-yasg版文档
pip install drf-yasg
项目 settings 设置
INSTALLED_APPS = [
...
'django.contrib.staticfiles', # required for serving swagger ui's css/js files
'drf_yasg',
...
]
项目urls设置
from django.urls import re_path
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
...
schema_view =