安裝
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ù)端挫剑,如果不寫(xiě)端口的話默認(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ù)庫(kù)配置
分別配置是使用sqlite3還是MySQL,默認(rèn)配置為sqlite3
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
如果要設(shè)置MySQL為數(shù)據(jù)庫(kù)森瘪,進(jìn)行如下設(shè)置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'books', #你的數(shù)據(jù)庫(kù)名稱(chēng)
'USER': 'root', #你的數(shù)據(jù)庫(kù)用戶(hù)名
'PASSWORD': '', #你的數(shù)據(jù)庫(kù)密碼
'HOST': '', #你的數(shù)據(jù)庫(kù)主機(jī)判哥,留空默認(rèn)為localhost
'PORT': '3306', #你的數(shù)據(jù)庫(kù)端口
}
}
添加應(yīng)用
確保settings文件的INSTALLED_APPS目錄下有你的應(yīng)用名稱(chē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ù)庫(kù))
在models.py 里面設(shè)計(jì)數(shù)據(jù)表 關(guān)系 业舍,使用語(yǔ)法使用orm語(yǔ)法來(lái)實(shí)現(xiàn)撤缴,比如要設(shè)計(jì)一個(gè)圖書(shū)的表
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)建一個(gè)表(前提是在models.py里面已經(jīng)寫(xiě)好了orm)
注意劫扒,在執(zhí)行這條命令前先確保Django版本的Python已經(jīng)安裝了模塊mysqlclient
如果沒(méi)有捐顷,應(yīng)該先執(zhí)行pip3 install mysqlclient ,然后再執(zhí)行以上語(yǔ)句 - python3 manage.py migrate
添加路由(urls)
路徑的添加在urls這個(gè)文件中引入荡陷,需要注意的是,其中的語(yǔ)法迅涮,比如別名废赞,數(shù)據(jù)提交,正則表達(dá)式
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', views.index),
url(r'^addbook/', views.addbook),
]
添加函數(shù)
函數(shù)的添加主要依靠的是request和HttpResponse叮姑,注意這兩個(gè)類(lèi)的用法唉地,
def index(request):
return render(request, 'index.html')
而且函數(shù)里面涉及到給數(shù)據(jù)庫(kù)添加和提取數(shù)據(jù),因此需要用到添加數(shù)據(jù)庫(kù)的方法传透,注意用models時(shí)的調(diào)用問(wèn)題
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ù)庫(kù)里面插入數(shù)據(jù)
Book.objects.create(
title=title,
price=price,
date=date,
publish=publish,
auth=auth
)
return redirect('/index/')
return render(request, 'addbook.html')
添加模板
添加模板一個(gè)是涉及靜態(tài)文件的引入耘沼,一個(gè)是設(shè)計(jì)模板語(yǔ)言,標(biāo)簽渲染以及變量渲染和循環(huán)朱盐、判斷群嗤,里面有好多語(yǔ)法
{% load staticfiles %} # 靜態(tài)文件的引入
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
# 通過(guò)使用別名來(lái)引入外部文件
<link rel="stylesheet" href="{% static 'dist/css/bootstrap.css' %}">
</head>
<body>
# a標(biāo)簽的跳轉(zhuǎn)
<a href="/addbook/"><button class="btn btn-primary">添加書(shū)籍</button></a>
<table class="table table-striped">
<tr>
<th>編號(hào)</th>
<th>姓名</th>
<th>價(jià)格</th>
<th>出版日期</th>
<th>出版社</th>
<th>作者</th>
</tr>
</table>
</body>
</html>