1.导入正则模块:re_path
2.settings配置静态资源
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static')]
3.settings 连接数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', #选择MySQL数据库
'NAME': 'test1', #数据库名
'USER': 'root',
'PASSWORD': 'ryy123456',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
4.views重定向
- redirect重定向关键字
from django.shortcuts import render,redirect from django.http import HttpResponse,HttpResponseRedirect
def hello(request):
#响应内容
#1.跟绝对路径 2.跟相对路径 3.HTTP
#return redirect('http://127.0.0.1/index/')
return HttpResponseRedirect('/index/')
5.网页添加标题
def index(request):
ctx = {'title':'首页'}
#重定向
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 = '首页'
#导入HTML文件 render from django.shortcuts import render
return render(request=request,template_name='index.html',context=locals())
6. init.py导入pymysql模块
import pymysql
pymysql.install_as_MySQLdb()
7.迁移
1.生成迁移文件 2.执行迁移
python manage.py makemigrations
python manage.py migrate