settings.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. """
  2. Django settings for invoice_ocr project.
  3. Generated by 'django-admin startproject' using Django 3.1.4.
  4. For more information on this file, see
  5. https://docs.djangoproject.com/en/3.1/topics/settings/
  6. For the full list of settings and their values, see
  7. https://docs.djangoproject.com/en/3.1/ref/settings/
  8. """
  9. import os
  10. import time
  11. from pathlib import Path
  12. # Build paths inside the project like this: BASE_DIR / 'subdir'.
  13. BASE_DIR = Path(__file__).resolve().parent.parent
  14. # Quick-start development settings - unsuitable for production
  15. # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
  16. # SECURITY WARNING: keep the secret key used in production secret!
  17. SECRET_KEY = 'l56929t$rpl&(75-g20nnoe*@q7gxp9c)i*1lh2fi=4+p-4(mj'
  18. # SECURITY WARNING: don't run with debug turned on in production!
  19. DEBUG = True
  20. ALLOWED_HOSTS = ['119.3.54.245', '127.0.0.1']
  21. # Application definition
  22. INSTALLED_APPS = [
  23. 'django.contrib.admin',
  24. 'django.contrib.auth',
  25. 'django.contrib.contenttypes',
  26. 'django.contrib.sessions',
  27. 'django.contrib.messages',
  28. 'django.contrib.staticfiles',
  29. 'corsheaders',
  30. 'ocr',
  31. ]
  32. MIDDLEWARE = [
  33. 'django.middleware.security.SecurityMiddleware',
  34. 'django.contrib.sessions.middleware.SessionMiddleware',
  35. 'corsheaders.middleware.CorsMiddleware', # 跨域资源
  36. 'django.middleware.common.CommonMiddleware',
  37. # 'django.middleware.csrf.CsrfViewMiddleware',
  38. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  39. 'django.contrib.messages.middleware.MessageMiddleware',
  40. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  41. ]
  42. ROOT_URLCONF = 'invoice_ocr.urls'
  43. TEMPLATES = [
  44. {
  45. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  46. 'DIRS': [],
  47. 'APP_DIRS': True,
  48. 'OPTIONS': {
  49. 'context_processors': [
  50. 'django.template.context_processors.debug',
  51. 'django.template.context_processors.request',
  52. 'django.contrib.auth.context_processors.auth',
  53. 'django.contrib.messages.context_processors.messages',
  54. ],
  55. },
  56. },
  57. ]
  58. WSGI_APPLICATION = 'invoice_ocr.wsgi.application'
  59. # Database
  60. # https://docs.djangoproject.com/en/3.1/ref/settings/#databases
  61. DATABASES = {
  62. 'default': {
  63. 'ENGINE': 'django.db.backends.sqlite3',
  64. 'NAME': BASE_DIR / 'db.sqlite3',
  65. }
  66. }
  67. # Password validation
  68. # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
  69. AUTH_PASSWORD_VALIDATORS = [
  70. {
  71. 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  72. },
  73. {
  74. 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
  75. },
  76. {
  77. 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  78. },
  79. {
  80. 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  81. },
  82. ]
  83. # Internationalization
  84. # https://docs.djangoproject.com/en/3.1/topics/i18n/
  85. LANGUAGE_CODE = 'en-us'
  86. TIME_ZONE = 'UTC'
  87. USE_I18N = True
  88. USE_L10N = True
  89. USE_TZ = True
  90. # 日志:当运行出错时,记录在日志中,方便后续修改
  91. cur_path = os.path.dirname(os.path.realpath(__file__)) # log_path是存放日志的路径
  92. log_path = os.path.join(os.path.dirname(cur_path), 'logs')
  93. if not os.path.exists(log_path):os.mkdir(log_path) # 如果不存在这个logs文件夹,就自动创建一个
  94. LOGGING = {
  95. 'version': 1,
  96. 'disable_existing_loggers': True,
  97. 'formatters': {
  98. # 日志格式
  99. 'standard': {
  100. 'format': '[%(asctime)s] [%(filename)s:%(lineno)d] [%(module)s:%(funcName)s] '
  101. '[%(levelname)s]- %(message)s'},
  102. 'simple': { # 简单格式
  103. 'format': '%(levelname)s %(message)s'
  104. },
  105. },
  106. # 过滤
  107. 'filters': {
  108. },
  109. # 定义具体处理日志的方式
  110. 'handlers': {
  111. # 默认记录所有日志
  112. 'default': {
  113. 'level': 'INFO',
  114. 'class': 'logging.handlers.RotatingFileHandler',
  115. 'filename': os.path.join(log_path, 'all-{}.log'.format(time.strftime('%Y-%m-%d'))),
  116. 'maxBytes': 1024 * 1024 * 5, # 文件大小
  117. 'backupCount': 5, # 备份数
  118. 'formatter': 'standard', # 输出格式
  119. 'encoding': 'utf-8', # 设置默认编码,否则打印出来汉字乱码
  120. },
  121. # 输出错误日志
  122. 'error': {
  123. 'level': 'ERROR',
  124. 'class': 'logging.handlers.RotatingFileHandler',
  125. 'filename': os.path.join(log_path, 'error-{}.log'.format(time.strftime('%Y-%m-%d'))),
  126. 'maxBytes': 1024 * 1024 * 5, # 文件大小
  127. 'backupCount': 5, # 备份数
  128. 'formatter': 'standard', # 输出格式
  129. 'encoding': 'utf-8', # 设置默认编码
  130. },
  131. # 控制台输出
  132. 'console': {
  133. 'level': 'DEBUG',
  134. 'class': 'logging.StreamHandler',
  135. 'formatter': 'standard'
  136. },
  137. # 输出info日志
  138. 'info': {
  139. 'level': 'INFO',
  140. 'class': 'logging.handlers.RotatingFileHandler',
  141. 'filename': os.path.join(log_path, 'info-{}.log'.format(time.strftime('%Y-%m-%d'))),
  142. 'maxBytes': 1024 * 1024 * 5,
  143. 'backupCount': 5,
  144. 'formatter': 'standard',
  145. 'encoding': 'utf-8', # 设置默认编码
  146. },
  147. },
  148. # 配置用哪几种 handlers 来处理日志
  149. 'loggers': {
  150. # 类型 为 django 处理所有类型的日志, 默认调用
  151. 'django': {
  152. 'handlers': ['default', 'console'],
  153. 'level': 'INFO',
  154. 'propagate': False
  155. },
  156. # log 调用时需要当作参数传入
  157. 'log': {
  158. 'handlers': ['error', 'info', 'console', 'default'],
  159. 'level': 'INFO',
  160. 'propagate': True
  161. },
  162. }
  163. }
  164. # 跨域增加忽略
  165. CORS_ALLOW_CREDENTIALS = True
  166. CORS_ORIGIN_ALLOW_ALL = True
  167. CORS_ORIGIN_WHITELIST = (
  168. 'http://*.*.*.*:*',
  169. )
  170. CORS_ALLOW_METHODS = (
  171. 'DELETE',
  172. 'GET',
  173. 'OPTIONS',
  174. 'PATCH',
  175. 'POST',
  176. 'PUT',
  177. 'VIEW',
  178. )
  179. CORS_ALLOW_HEADERS = (
  180. 'XMLHttpRequest',
  181. 'X_FILENAME',
  182. 'accept-encoding',
  183. 'authorization',
  184. 'content-type',
  185. 'dnt',
  186. 'origin',
  187. 'user-agent',
  188. 'x-csrftoken',
  189. 'x-requested-with',
  190. )
  191. STATIC_URL = '/static/'