TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [ os.path.join(BASE_DIR, 'templates1'), # 根目錄->templates1 os.path.join(BASE_DIR, 'templates2'), # 根目錄->templates2 ], # 配置render()函數在上述路徑下尋找模板 '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', ], }, }, ]
# 靜態文件保存目錄的別名 STATIC_URL = '/static/' # 靜態文件存放文件夾 STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static1"), os.path.join(BASE_DIR, "static2"), ] # 上述配置的效果就是 請求指定別名會在指定目錄下尋找指定文件 # 例:127.0.0.1:8000/static/test.css 會在STATICFILES_DIRS節中配置的每一個目錄尋找名爲test.css的文件
1 DATABASES = { 2 'default': { 3 'ENGINE': 'django.db.backends.mysql', # 鏈接的數據庫類型 4 'HOST': '127.0.0.1', # 鏈接數據庫的地址 5 'PORT': 3306, # 端口 6 'NAME': "testdb", # 數據庫名稱 7 'USER': 'root', # 用戶 8 'PASSWORD': 'root' # 密碼 9 } 10 }
1 LOGGING = { 2 'version': 1, 3 'disable_existing_loggers': False, 4 'handlers': { 5 'console': { 6 'level': 'DEBUG', 7 'class': 'logging.StreamHandler', 8 }, 9 }, 10 'loggers': { 11 'django.db.backends': { 12 'handlers': ['console'], 13 'propagate': True, 14 'level': 'DEBUG', 15 }, 16 } 17 }
SESSION_ENGINE = 'django.contrib.sessions.backends.db'
SESSION_ENGINE = 'django.contrib.sessions.backends.cache' # 引擎 SESSION_CACHE_ALIAS = 'default' # 使用的緩存別名(默認內存緩存,也能夠是memcache),此處別名依賴緩存的設置
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
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 = False # 是否每次請求都保存Session,默認修改以後才保存(默認)
LOGIN_URL = '/login/' # 這裏配置成你項目登陸頁面的路由
AUTH_USER_MODEL = "app名.User表名"
pip install django-cors-headers
INSTALLED_APPS = ( 'corsheaders', ) MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', ] CORS_ORIGIN_WHITELIST = ( '127.0.0.1:8080', 'localhost:8080', #凡是出如今白名單中的域名,均可以訪問後端接口 ) CORS_ALLOW_CREDENTIALS = True # 指明在跨域訪問中,後端是否支持對cookie的操做
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>文件上傳測試</title> 6 </head> 7 <body> 8 <form action="/img_upload/" method="post" enctype="multipart/form-data"> 9 <p>title: <input type="text" name="title"></p> 10 <p>img: <input type="file" name="file" accept="image/*"></p> 11 <p><input type="submit" value="submit"></p> 12 <p><img src="{{ img_link }}"></p> 13 </form> 14 </body> 15 </html>
1 from django.db import models 2 3 4 class Image(models.Model): 5 title = models.CharField(max_length=100, verbose_name=u'標題') 6 # upload_to參數可指定文件保存位置 (相對配置中的MEDIA_ROOT對應路徑) 7 image = models.ImageField(upload_to='image/%Y/%m/%d/%H/%M/%S', max_length=400) 8 9 def __str__(self): 10 return self.title
1 from django.conf.urls import url 2 from img_upload import views 3 from django.views.static import serve # 導入相關靜態文件處理的views控制包 4 from django_test.settings import MEDIA_ROOT # 導入項目文件夾中setting中的MEDIA_ROOT絕對路徑 5 6 urlpatterns = [ 7 url(r'^img_upload/', views.img_upload), 8 url(r'^media/(?P<path>.*)$', serve, {"document_root": MEDIA_ROOT}), 9 ]
1 from django.shortcuts import render 2 from img_upload import models 3 4 5 def img_upload(request): 6 img_link = '' 7 if request.method == 'POST': 8 title = request.POST.get('title') 9 img_file = request.FILES.get('file') 10 img_obj = models.Image.objects.create(title=title, image=img_file) 11 img_link = img_obj.image.url 12 return render(request, 'img_upload.html', locals())
1 MEDIA_URL = '/media/' 2 MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 3 4 STATIC_URL = '/static/' 5 STATICFILES_DIRS = [ 6 os.path.join(BASE_DIR, 'static'), 7 os.path.join(BASE_DIR, 'media'), 8 ]