django如何应对sql注入攻击

django如何应对sql注入攻击

django应对sql注入攻击的方法:

1.使用django自带的数据库API,它会根据数据库的转换规则,自动转义特殊的SQL参数。

2.在cursor.execute()的sql语句中使用“%s”,而不要在sql内直接添加参数,例如:

from django.db import connection

def user_contacts(request):

user = request.GET['username']

sql = "SELECT * FROM user_contacts WHERE username = %s"

cursor = connection.cursor()

cursor.execute(sql, [user])

# ... do something with the results

相关文章

django跟redis交互的示例:在django项目同名文件夹下的setti...
django应对sql注入攻击的方法:1.使用django自带的数据库API...
在django中引入jquery的方法:1.下载jquery源文件;2.新建st...
用django搭建网站的操作方法:1.快捷键win+r,打开运行窗口,...
如果 PyCharm 无法创建 Django 项目,可以尝试以下解决方法:...
要在PyCharm中安装Django框架,请按照以下步骤进行操作:1. ...