安裝
pip3 install django
創(chuàng)建Django項(xiàng)目
- django-admin startproject mysite(可以創(chuàng)建在任何目錄)
- cd mysite(進(jìn)入目錄)
- python3 manage.py startapp blog 在項(xiàng)目中創(chuàng)建程序
- python3 manage.py runserver 127.0.0.1:8080(運(yùn)行服務(wù)端,如果不寫端口的話默認(rèn)為8000)
檢查配置信息
在settings文件里面配置模板路徑
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',
],
},
},
]
配置靜態(tài)文件目錄
- 創(chuàng)建Python package包,命名為static
- 然后根據(jù)static的位置配置靜態(tài)文件路徑
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
)
- 數(shù)據(jù)庫配置
分別配置是使用sqlite3還是MySQL,默認(rèn)配置為sqlite3
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
如果要設(shè)置MySQL為數(shù)據(jù)庫,進(jìn)行如下設(shè)置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'books', #你的數(shù)據(jù)庫名稱
'USER': 'root', #你的數(shù)據(jù)庫用戶名
'PASSWORD': '', #你的數(shù)據(jù)庫密碼
'HOST': '', #你的數(shù)據(jù)庫主機(jī),留空默認(rèn)為localhost
'PORT': '3306', #你的數(shù)據(jù)庫端口
}
}
添加應(yīng)用
確保settings文件的INSTALLED_APPS目錄下有你的應(yīng)用名稱
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bookmanage.apps.BookmanageConfig',
]
設(shè)計(jì)表關(guān)系 (數(shù)據(jù)庫)
在models.py 里面設(shè)計(jì)數(shù)據(jù)表 關(guān)系 ,使用語法使用orm語法來實(shí)現(xiàn),比如要設(shè)計(jì)一個圖書的表
class Book(models.Model):
title = models.CharField(max_length=32)
price = models.IntegerField()
date = models.DateField()
publish = models.CharField(max_length=32)
author = models.CharField(max_length=32)
- python3 manage.py makemigrations
創(chuàng)建一個表(前提是在models.py里面已經(jīng)寫好了orm)
注意,在執(zhí)行這條命令前先確保Django版本的Python已經(jīng)安裝了模塊mysqlclient
如果沒有,應(yīng)該先執(zhí)行pip3 install mysqlclient ,然后再執(zhí)行以上語句 - python3 manage.py migrate
添加路由(urls)
路徑的添加在urls這個文件中引入,需要注意的是,其中的語法,比如別名,數(shù)據(jù)提交,正則表達(dá)式
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', views.index),
url(r'^addbook/', views.addbook),
]
添加函數(shù)
函數(shù)的添加主要依靠的是request和HttpResponse,注意這兩個類的用法,
def index(request):
return render(request, 'index.html')
而且函數(shù)里面涉及到給數(shù)據(jù)庫添加和提取數(shù)據(jù),因此需要用到添加數(shù)據(jù)庫的方法,注意用models時的調(diào)用問題
from django.shortcuts import render, redirect
from bookmanage.models import *
def addbook(request):
if request.method == 'POST':
title = request.POST.get('title')
price = request.POST.get('price')
date = request.POST.get('date')
publish = request.POST.get('publish')
auth = request.POST.get('auth')
# 想數(shù)據(jù)庫里面插入數(shù)據(jù)
Book.objects.create(
title=title,
price=price,
date=date,
publish=publish,
auth=auth
)
return redirect('/index/')
return render(request, 'addbook.html')
添加模板
添加模板一個是涉及靜態(tài)文件的引入,一個是設(shè)計(jì)模板語言,標(biāo)簽渲染以及變量渲染和循環(huán)、判斷,里面有好多語法
{% load staticfiles %} # 靜態(tài)文件的引入
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
# 通過使用別名來引入外部文件
<link rel="stylesheet" href="{% static 'dist/css/bootstrap.css' %}">
</head>
<body>
# a標(biāo)簽的跳轉(zhuǎn)
<a href="/addbook/"><button class="btn btn-primary">添加書籍</button></a>
<table class="table table-striped">
<tr>
<th>編號</th>
<th>姓名</th>
<th>價格</th>
<th>出版日期</th>
<th>出版社</th>
<th>作者</th>
</tr>
</table>
</body>
</html>