python django教程 Django 配置

2021-10-22 11:05:33 字數 1775 閱讀 9002

django 配置

執行 django-admin.py startproject [project-name] 命令會生成一系列檔案,在django 1.6版本以後的 settings.py 檔案中有以下語句:# build paths inside the project like this: os.path.join(base_dir, ...)

import os

base_dir = os.path.dirname(os.path.dirname(__file__))

這裡用到了python中乙個神奇的變數 __file__ 這個變數可以獲取到當前檔案(包含這個**的檔案)的路徑。os.path.dirname(__file__) 得到檔案所在目錄,再來乙個os.path.dirname()就是目錄的上一級,base_dir 即為 專案 所在目錄。我們在後面的與目錄有關的變數都用它,這樣使得移植性更強。

# security warning: don't run with debug turned on in production!

debug = true

template_debug = true

debug=true時,如果出現 bug 便於我們看見問題所在,但是部署時最好不要讓使用者看見bug的詳情,可能一些不懷好心的人攻擊**,造成不必要的麻煩。

allowed_hosts = ['*.besttome.com','www.ziqiangxuetang.com']

allowed_hosts 允許你設定哪些網域名稱可以訪問,即使在 apache 或 nginx 等中繫結了,這裡不允許的話,也是不能訪問的。

當 debug=false時,這個為必填項,如果不想輸入,可以用 allow_hosts = ['*'] 來允許所有的。

static_url = '/static/'

static_root = os.path.join(base_dir,'static')

static 是靜態檔案所有目錄,比如 jquery.js, bootstrap.min.css 等檔案。

os.path.join(base_dir, "common_static"),

'/var/www/static/',

這樣我們就可以把靜態檔案放在 common_static 和 /var/www/static/中了,django也能找到它們。

media_url = '/media/'

media_root = os.path.join(base_dir,'media')

media資料夾用來存放使用者上傳的檔案,與許可權有關,詳情見  django 靜態檔案 和   django 部署

django 1.5 - django 1.7template_dirs = (

os.path.join(base_dir, 'templates').replace('\\', '/'),

os.path.join(base_dir, 'templates2').replace('\\', '/'),

django 1.8 及以上版本templates = [

'backend': 'django.template.backends.django.djangotemplates',

'dirs': [

os.path.join(base_dir,'templates').replace('\\', '/'),

os.path.join(base_dir,'templates2').replace('\\', '/'),

這樣 就可以把模板檔案放在 templates 和 templates2 資料夾中了。

python Django安裝教程

2.解壓到桌面 3 開啟cmd cd到這個目錄執行 python setup.py install命令即可安裝,安裝成功提示 installed d python27 lib site packages pytz 2018.5 py2.7.egg finished processing depend...

Python Django基礎教程(一)(入門)

django是比較流程的web框架之一。django安裝可以直接用pip安裝即可。環境windows py3.5版本 django 1.11 歡迎加入學習交流qq群 657341423 cmd下建立 專案建立 e djtest django admin.py startproject djtest ...

C 遊戲開發教程 D3D

本文適合有一定程式設計基礎的愛好者!本文不會涉及基本的語法等內容,本文適合所有遊戲開發初學者,本文從microsoft directx 9.0 sdk summer 2004 中的d3d下tutorials資料夾下的例子開始!關鍵字 c 遊戲開發 3d 教程 c 讀作 c sharp 是一種簡單 現...