Django配置-settings.py

settings

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import os
 
#向上找兩級,獲取基地址
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
SECRET_KEY = 'tt^-bhdmqd-^pet%puk*q58gcqg-g)9h+97!4m9aveddp_$%5e'
 
DEBUG = True
 
ALLOWED_HOSTS = []
 
#APP信息,本身新建的項目若是沒有自動加入,能夠再次手動加進去
INSTALLED_APPS = [
     'django.contrib.admin' ,
     'django.contrib.auth' ,
     'django.contrib.contenttypes' ,
     'django.contrib.sessions' ,
     'django.contrib.messages' ,
     'django.contrib.staticfiles' ,
     'app01' ,
     'backend' ,
     'web' ,
]
 
#中間件配置
MIDDLEWARE = [
     'django.middleware.security.SecurityMiddleware' ,
     'django.contrib.sessions.middleware.SessionMiddleware' ,
     'django.middleware.common.CommonMiddleware' ,
     'django.middleware.csrf.CsrfViewMiddleware' ,
     'django.contrib.auth.middleware.AuthenticationMiddleware' ,
     'django.contrib.messages.middleware.MessageMiddleware' ,
     'django.middleware.clickjacking.XFrameOptionsMiddleware' ,
     # "app01.my_middlewares.AuthMiddleware",  #注意本身寫的中間件,配置要寫在系統中的後面
]
 
ROOT_URLCONF = 'myblog.urls'
 
#模板(HTML頁面)路徑相關配置
TEMPLATES = [
     {
         'BACKEND' : 'django.template.backends.django.DjangoTemplates' ,
         'DIRS' : [os.path.join(BASE_DIR, 'templates' )],
         'APP_DIRS' : True ,
         'OPTIONS' : {
             'context_processors' : [
                 'django.template.context_processors.debug' ,
                 'django.template.context_processors.request' ,
                 'django.contrib.auth.context_processors.auth' ,
                 'django.contrib.messages.context_processors.messages' ,
             ],
         },
     },
]
 
# #配置模板
# TEMPLATE_DIRS = (
#         os.path.join(BASE_DIR,'templates'),
# )
 
#網頁服務網關協議消息
WSGI_APPLICATION = 'myblog.wsgi.application'
 
 
#原來默認的sqlite數據庫
# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#     }
# }
 
#mysql數據庫 同時須要設置__init__.py
# import pymysql
# pymysql.install_as_MySQLdb()
 
DATABASES = {
                 'default' : {
                     'ENGINE' : 'django.db.backends.mysql' ,
                     'NAME' : 'myblog' , #數據庫名
                     'USER' : 'root' ,
                     'PASSWORD' : '',
                     'HOST' : 'localhost' ,
                     'PORT' : 3306 ,
                     }
                 }
 
#認證相關
AUTH_PASSWORD_VALIDATORS = [
     {
         'NAME' : 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator' ,
     },
     {
         'NAME' : 'django.contrib.auth.password_validation.MinimumLengthValidator' ,
     },
     {
         'NAME' : 'django.contrib.auth.password_validation.CommonPasswordValidator' ,
     },
     {
         'NAME' : 'django.contrib.auth.password_validation.NumericPasswordValidator' ,
     },
]
 
# 語言
LANGUAGE_CODE = 'zh-hans'  # 主要針對admin頁面
# 時間
TIME_ZONE = 'Asia/Shanghai'
 
USE_I18N = True
USE_L10N = True
 
#時區設置
USE_TZ = False
 
#靜態文件
STATIC_URL = '/static/'  # 靜態文件別名
STATICFILES_DIRS = (
     os.path.join(BASE_DIR, 'static' ),  #主文件下靜態文件
)
 
# 與用戶上傳相關的配置
#路由urls.py中也須要配置
"""
# media配置:
from django.urls import path,re_path
from django.views.static import serve
from django.conf import settings
 
urlpatterns = [
     re_path(r"media/(?P<path>.*)$",serve,{"document_root":settings.MEDIA_ROOT}),
]
"""
 
MEDIA_ROOT = os.path.join(BASE_DIR, "media" )
MEDIA_URL = "/media/"
 
# 引用Django自帶的User表,繼承使用時須要設置
#若是數據庫中的UserInfo(用戶表)繼承django內置AbstractUser
#model需導入 from django.contrib.auth.models import AbstractUser
AUTH_USER_MODEL = "app01.UserInfo"  #應用名.UserInfo
 
# 自定義配置登錄驗證返回路徑
LOGIN_URL = "/login/"
LOGIN_REDIRECT_URL = '/'
LOGOUT_URL = '/logout/'
 
 
 
 
#
EMAIL_HOST = 'smtp.exmail.qq.com'  # 若是是 163 改爲 smtp.163.com
EMAIL_PORT = 465
EMAIL_HOST_USER = ''           # 賬號
EMAIL_HOST_PASSWORD = ''  # 密碼
# DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_USE_SSL = True
 
#session存儲的相關配置
# Django默認支持Session,而且默認是將Session數據存儲在數據庫中,即:django_session表中。提供了5種類型的Session
"""
#1.數據庫
SESSION_ENGINE = 'django.contrib.sessions.backends.db'  # 引擎(默認)
#2.緩存
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'  # 引擎
SESSION_CACHE_ALIAS = 'default'
# 要配合Django緩存配置
# 使用的緩存別名(默認內存緩存,也能夠是memcache),此處別名依賴緩存的設置
#3.文件
SESSION_ENGINE = 'django.contrib.sessions.backends.file'    # 引擎
SESSION_FILE_PATH = None
# 緩存文件路徑,若是爲None,則使用tempfile模塊獲取一個臨時地址tempfile.gettempdir()
#4.緩存+數據庫
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'        # 引擎
#5.加密cookie 基本沒有用
SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'   # 引擎
"""
 
# SESSION_COOKIE_NAME = "sessionid"                       # Session的cookie保存在瀏覽器上時的key,即:sessionid=隨機字符串(默認)
# SESSION_COOKIE_PATH = "/"                               # Session的cookie保存的路徑(默認)
# SESSION_COOKIE_DOMAIN = None                             # Session的cookie保存的域名(默認)
# SESSION_COOKIE_SECURE = False                            # 是否Https傳輸cookie(默認)
# SESSION_COOKIE_HTTPONLY = True                           # 是否Session的cookie只支持http傳輸(默認)
# SESSION_COOKIE_AGE = 1209600                             # Session的cookie失效日期(2周)(默認)
# SESSION_EXPIRE_AT_BROWSER_CLOSE = False                  # 是否關閉瀏覽器使得Session過時(默認)
# SESSION_SAVE_EVERY_REQUEST = True        # (每次刷新更新時間) 是否每次請求都保存Session,默認修改以後才保存(默認)
 
SESSION_COOKIE_AGE = 60 * 60 * 24
SESSION_SAVE_EVERY_REQUEST = True
 
#打印日誌到屏幕
LOGGING = {
     'version' : 1 ,
     'disable_existing_loggers' : False ,
     'handlers' : {
         'console' :{
             'level' : 'DEBUG' ,
             'class' : 'logging.StreamHandler' ,
         },
     },
     'loggers' : {
         'django.db.backends' : {
             'handlers' : [ 'console' ],
             'propagate' : True ,
             'level' : 'DEBUG' ,
         },
     }
}

views.py 發郵件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def comment(request):
     """
     提交評論視圖函數
     功能:
     1 保存評論
     2 建立事務
     3 發送郵件
     :param request:
     :return:
     """
     print (request.POST)
 
     article_id = request.POST.get( "article_id" )
     pid = request.POST.get( "pid" )
     content = request.POST.get( "content" )
     user_id = request.user.pk
 
     article_obj = models.Article.objects. filter (pk = article_id).first()
 
     # 事務操做
     with transaction.atomic():
         comment_obj = models.Comment.objects.create(user_id = user_id, article_id = article_id, content = content,
                                                     parent_comment_id = pid)
         models.Article.objects. filter (pk = article_id).update(comment_count = F( "comment_count" ) + 1 )
 
     response = {}
 
     response[ "create_time" ] = comment_obj.create_time.strftime( "%Y-%m-%d %X" )
     response[ "username" ] = request.user.username
     response[ "content" ] = content
 
     # 發送郵件
     from django.core.mail import send_mail
     from django.conf import settings
 
     # send_mail(
     #     "您的文章%s新增了一條評論內容"%article_obj.title,
     #     content,
     #     settings.EMAIL_HOST_USER,
     #     ["916852314@qq.com"]
     # )
 
     import threading
     t = threading.Thread(target = send_mail, args = ( "您的文章%s新增了一條評論內容" % article_obj.title,
                                                  content,
                                                  settings.EMAIL_HOST_USER,
                                                  [ "916852314@qq.com" ])
                          )
     t.start()
 
     return JsonResponse(response)
相關文章
相關標籤/搜索