try: user_email = UserInfo.objects.filter(email__exact = emai ) print(user_email)
时间: 2024-06-02 08:09:00 浏览: 7
在这段代码中,你尝试从 `UserInfo` 模型中过滤出 `email` 字段值等于 `emai` 的记录,并将结果赋值给 `user_email` 变量。然后你打印了 `user_email` 变量的值。
需要注意的是,`email__exact` 表示精确匹配 `email` 字段的值,即只会返回 `email` 字段值与 `emai` 完全相等的记录。如果你想进行模糊匹配,可以使用 `email__contains` 或者 `email__icontains` 来代替。
相关问题
register_exist(request): username = request.GET.get('uname') uemail = request.GET.get('uemail') count = UserInfo.objects.filter(uname=username).count() email_count=UserInfo.objects.filter(uemail=uemail).count() print(email_count) return JsonResponse({'count': count,'email_count':email_count})
这是一个Django的视图函数,用于判断注册信息是否已存在。根据传入的请求对象,获取GET参数中的用户名和邮箱。然后分别使用Django的ORM框架查询数据库中是否已存在该用户名和邮箱,将结果以JSON格式返回给前端。其中,count表示用户名已存在的数量,email_count表示邮箱已存在的数量。这个函数的作用是在用户注册时,实时检查输入的信息是否已被其他用户占用。
def init_known_face(self): # 数据库连接对象 conn = MySQLdb.connect(host = 'localhost',port=3306 ,user = 'root',passwd = 'root',db = 'attendanceinfo',charset='utf8') # 游标对象 cur = conn.cursor() # 查询的sql语句 sql = "SELECT * FROM userinfo " cur.execute(sql) data = cur.fetchall() row=cur.rowcount vol=6 self.stopvidow = False self.known_face_encodings = [] self.known_face_names = [] for i in range(row): is_exsit = os.path.exists(str(data[i][4])) if (is_exsit): obama_image = face_recognition.load_image_file(str(data[i][4])) obama_face_encoding = self.new_method(obama_image) self.known_face_encodings.append(obama_face_encoding) self.known_face_names.append(str(data[i][1])) cur.close() self.new_method(conn) self.new_method2() def new_method(self, conn): conn.close() def new_method1(self, obama_image): obama_face_encoding = face_recognition.face_encodings(obama_image)[0] return obama_face_encoding找出代码错误并修正
在这段代码中,`self.new_method` 和 `self.new_method2` 方法没有被正确命名,应该修改为正确的方法名。同时,`self.new_method` 方法的作用是关闭数据库连接,但是在初始化已知人脸编码时被错误调用,应该将其修改为一个专门关闭数据库连接的方法。
修改后的代码如下:
```python
import face_recognition
import MySQLdb
import os
class FaceRecognizer:
def __init__(self):
self.stopvidow = False
self.known_face_encodings = []
self.known_face_names = []
self.init_known_face()
def init_known_face(self):
# 数据库连接对象
conn = MySQLdb.connect(host='localhost', port=3306, user='root',
passwd='root', db='attendanceinfo', charset='utf8')
# 游标对象
cur = conn.cursor()
# 查询的sql语句
sql = "SELECT * FROM userinfo "
cur.execute(sql)
data = cur.fetchall()
row = cur.rowcount
vol = 6
self.known_face_encodings = []
self.known_face_names = []
for i in range(row):
is_exsit = os.path.exists(str(data[i][4]))
if (is_exsit):
obama_image = face_recognition.load_image_file(str(data[i][4]))
obama_face_encoding = self.get_face_encoding(obama_image)
self.known_face_encodings.append(obama_face_encoding)
self.known_face_names.append(str(data[i][1]))
cur.close()
conn.close()
def get_face_encoding(self, obama_image):
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
return obama_face_encoding
def new_method2(self):
pass
```
注意,我将 `new_method` 修改为 `get_face_encoding`,并在 `init_known_face` 方法中调用它来获取人脸编码。此外,我也将 `new_method2` 修改为一个空方法。