Django三板斧&request请求&连接MySQL
2022/2/25 2:22:27
本文主要是介绍Django三板斧&request请求&连接MySQL,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
内容概要
- Django三板斧
- 静态文件配置
- request请求
- form表单
- Django连接MySQL
内容详细
Django三板斧
1. HttpResponse 2. render 3. redirect def index(request): print('123') # 暂且记忆返回值是字符串类型 # return HttpResponse('hello django!') # 返回给浏览器了 ctime = time.strftime('%Y-%m-%d %X') a = 1 b = 2 print(locals()) # return render(request, 'index.html', {'ctime11111':ctime, 'a':1, 'b':2}) # return render(request, 'index.html', locals()) # return redirect('http://www.baidu.com') return redirect('/admin/') # ip+port/admin
静态文件配置
什么是静态文件 css, js, jq, bootstrap, img... eg: 以登录功能为例 # 静态文件的存储路径一般是static,默认是没有这个文件夹的,所以,需要我们自己手动创建出来这个文件夹 # 在static文件夹中还可以继续根据不同的功能进行划分 js css lib img # http://127.0.0.1:8000/static/css/bootstrap.min.css不能访问是因为后盾没有开放可访问的路径 STATIC_URL = '/aaaaa/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), # 这样一配置完成,静态路径的根就是static os.path.join(BASE_DIR, 'static1'), # 这样一配置完成,静态路径的根就是static os.path.join(BASE_DIR, 'static2'), # 这样一配置完成,静态路径的根就是static ]
form表单
<form action=""> ''' 1. 什么都不写,提交到当前页面 2. 全写:https://passport.baidu.com/v2/api/?login 3. 只写后缀 /login/ => 自动补全ip和端口 http://127.0.0.1:8000/login/ ''' 面试题:get和post的区别? http://127.0.0.1:8000/login/?a=1&b=2&c=3 ''' get请求: 1. get没有请求体 2. 对数据大小的限制是4KB 3. 不安全 post请求: 1. post才有请求体 2. 对数据大小没有限制 3. 安全 ''' ''' MySQL中可能会出现的安全问题:SQL注入 前端当中可能会出现的安全问题:xss攻击 django中会出现的安全问题:csrf跨站请求 ''' def login(request): # 1. 如何判断请求方式? print(request) # <WSGIRequest: GET '/login/'> print(request.method, type(request.method)) # GET <WSGIRequest: GET '/login/'> if request.method == 'POST': # 2. 如何获取post请求方式的数据? # print(request.POST) # QueryDict: {'user': ['ly'], 'password': ['123']}> # print(request.POST.get('username')) # ly # # get只能拿最后一个值 # print(request.POST.get('hobby')) # ly # print(request.POST.getlist('hobby')) # 拿到所有的值 username = request.POST.get('username') password = request.POST.get('password') if username == 'ly' and password =='123': return HttpResponse('登录成功') else: return HttpResponse('密码错误') return render(request, 'login.html', )
Django连接MySQL
# 连接mysql的配置 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'db8_3', 'HOST': '127.0.0.1', 'PORT': 3306, 'USER': 'root', 'PASSWORD':'123', 'CHARSET':'utf8' } # django默认的操作mysql的模块是MySQLdb ''' 在python3.6版本以下,需要加入下面两句话 需要先安装pymysql pip3 install pymysql import pymysql pymysql.install_as_MySQLdb() '''
这篇关于Django三板斧&request请求&连接MySQL的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-21MySQL集群部署资料:新手入门教程
- 2024-09-21MySQL集群资料:初学者入门指南
- 2024-09-21部署MySQL集群资料:新手入门教程
- 2024-09-20MySQL集群部署教程:新手入门指南
- 2024-09-20MySQL集群教程:初学者必备指南
- 2024-09-20部署MySQL集群项目实战:新手入门教程
- 2024-09-20如何部署MySQL集群:简单教程
- 2024-09-20MySQL集群部署:新手入门指南
- 2024-09-20部署MySQL集群学习:入门指南
- 2024-09-20部署MySQL集群入门教程