1.導(dǎo)入正則模塊:re_path
2.settings配置靜態(tài)資源
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static')]
3.settings 連接數(shù)據(jù)庫(kù)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', #選擇MySQL數(shù)據(jù)庫(kù)
'NAME': 'test1', #數(shù)據(jù)庫(kù)名
'USER': 'root',
'PASSWORD': 'ryy123456',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
4.views重定向
- redirect重定向關(guān)鍵字
from django.shortcuts import render,redirect from django.http import HttpResponse,HttpResponseRedirect
def hello(request):
#響應(yīng)內(nèi)容
#1.跟絕對(duì)路徑 2.跟相對(duì)路徑 3.HTTP
#return redirect('http://127.0.0.1/index/')
return HttpResponseRedirect('/index/')
5.網(wǎng)頁(yè)添加標(biāo)題
def index(request):
ctx = {'title':'首頁(yè)'}
#重定向
return render(request=request,template_name='index.html',context=ctx)
type_list = models.Product.objects.values('type').distinct() #distinct()去重
name_list = models.Product.objects.values('name','type')
title = '首頁(yè)'
#導(dǎo)入HTML文件 render from django.shortcuts import render
return render(request=request,template_name='index.html',context=locals())
6. init.py導(dǎo)入pymysql模塊
import pymysql
pymysql.install_as_MySQLdb()
7.遷移
1.生成遷移文件 2.執(zhí)行遷移
python manage.py makemigrations
python manage.py migrate