django創建管理系統之一----準備工做

django 安裝:html

1.在線安裝國內通常使用豆瓣的源:pip install django  -i http://pypi.douban.com/simple/   或者  pip  install  -i  https://pypi.doubanio.com/simple/  --trusted-host pypi.doubanio.com  djangopython

2.離線安裝:django

 

1:python 模塊 pip的離線安裝: 
a.下載該模塊的tar包
b.解壓
c.進入setup所在目錄並執行:  pip install -e .    , 這樣就安裝成功了:
 
django 加入環境變量(win): 
path中加入django的實際路徑 例如:  C:\Python34\Lib\site-packages\django;
 
使用django創建project 和app:
django-admin startproject my_djangoproject  
cd my_djangoproject
python manage.py  startapp app01
啓動django項目並查看:
python manage.py runserver 127.0.0.1:8000
 
創建管理系統 
-----------------------------------------------
配置修改:
-----------------------------------------------
app01加入 settings 
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01',
]

static basedir 設定:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
  模板設定
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',
],
},
},
]

seesion設定:
settings.py
SESSION_ENGINE = 'django.contrib.sessions.backends.db'  # 引擎(默認)
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,默認修改以後才保存(默認)
-----------------------------------------------
創建網頁測試:
-----------------------------------------------
1.創建templates 目錄,創建網頁,創建路由,創建視圖函數。
 

urls.py add:瀏覽器

re_path('login.html', views.Login),安全

views.py add:cookie

from django.urls import path,re_path
from app01 import views
def Login(request):
return render(request,"login.html")
login.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
hello login
</body>
</html>
2.建立登陸頁login.html屏蔽form 安全設置:
settings:
# 'django.middleware.csrf.CsrfViewMiddleware',
3.創建數據模型:
class Classes(models.Model):
caption = models.CharField(max_length=32)

class Student(models.Model):
name = models.CharField(max_length=32)
email = models.CharField(max_length=32,null=True)
cls = models.ForeignKey('Classes',on_delete=models.CASCADE,)

class Teacher(models.Model):
name = models.CharField(max_length=32)
cls = models.ManyToManyField('Classes')
class Administrator(models.Model):
username = models.CharField(max_length=32)
password = models.CharField(max_length=32)
python manage.py makemigrationspython manage.py migrate
相關文章
相關標籤/搜索