def signal(request): obj = models.User(user='******') print('end') obj.save() obj = models.User(user='******') obj.save() obj = models.User(user='******') obj.save() pizza_done.send(sender='seven', toppings=123, size=456) #触发自定义信号 return HttpResponse('测试')
def register(request): if request.session.get('is_login', None): return redirect('/index/') if request.method == 'POST': username = request.POST.get('Name') email = request.POST.get('Email') tel = request.POST.get('Tel') password = request.POST.get('password') confirm_password = request.POST.get('confirm_password') message = '请检查填写的内容!' if password != confirm_password: message = '两次输入密码不一致!' return render(request, 'app01/register.html', {'message': message}) else: same_name_user = models.User.objects.filter(user=username) if same_name_user: message = '用户名已经存在' return render(request, 'app01/register.html', {'message': message}) same_email_user = models.User.objects.filter(mail=email) if same_email_user: message = '该邮箱已经被注册了!' return render(request, 'app01/register.html', {'message': message}) new_user = models.User() new_user.user = username new_user.passwd = password new_user.mail = email new_user.tel = tel new_user.lx = "1" new_user.save() message = "注册成功!" return render(request, 'app01/register.html', {'message': message}) return render(request, 'app01/register.html')
def register(request): """ 注册视图 :param request: :return: """ if request.session.get('is_login', None): return redirect('/showbooks/') if request.method == 'POST': register_form = forms.RegisterForm(request.POST) message = "请检查填写的内容!" if register_form.is_valid(): username = register_form.cleaned_data.get('username') password1 = register_form.cleaned_data.get('password1') password2 = register_form.cleaned_data.get('password2') email = register_form.cleaned_data.get('email') sex = register_form.cleaned_data.get('sex') if password1 != password2: message = '两次输入的密码不同!' return render(request, 'register.html', locals()) else: same_name_user = models.User.objects.filter(name=username) if same_name_user: message = '用户名已经存在' return render(request, 'register.html', locals()) same_email_user = models.User.objects.filter(email=email) if same_email_user: message = '该邮箱已经被注册了!' return render(request, 'register.html', locals()) new_user = models.User() new_user.name = username new_user.password = hash_code(password1) new_user.email = email new_user.sex = sex new_user.save() else: return render(request, 'register.html', locals()) register_form = forms.RegisterForm() return render(request, 'register.html', locals())
def register(request): if request.session.get('is_login', None): # 登录状态不允许注册。你可以修改这条原则! return redirect("/login/") if request.method == "POST": register_form = forms.RegisterForm(request.POST) message = "请检查填写的内容!" if register_form.is_valid(): # 获取数据 username = register_form.cleaned_data['username'] password1 = register_form.cleaned_data['password1'] password2 = register_form.cleaned_data['password2'] email = register_form.cleaned_data['email'] # sex = register_form.cleaned_data['sex'] # print(sex) if password1 != password2: # 判断两次密码是否相同 message = "两次输入的密码不同!" return render(request, 'login/register.html', locals()) else: same_name_user = models.User.objects.filter(name=username) if same_name_user: # 用户名唯一 message = '用户已经存在,请重新选择用户名!' return render(request, 'login/register.html', locals()) same_email_user = models.User.objects.filter(email=email) if same_email_user: # 邮箱地址唯一 message = '该邮箱地址已被注册,请使用别的邮箱!' return render(request, 'login/register.html', locals()) # 当一切都OK的情况下,创建新用户 new_user = models.User() new_user.name = username new_user.password = password1 new_user.email = email # new_user.sex = sex new_user.save() return redirect('/login/') # 自动跳转到登录页面 register_form = forms.RegisterForm() return render(request, 'login/register.html', locals())
def reg(request): now_page = '注册' now_tou = 't_reg' d1 = my_funcs.get_tou(request, now_tou) d1['now_page'] = now_page if request.is_ajax(): if request.method == 'POST': def_type = request.POST.get('def_type') send_data = {'stus': 201, 'data': None} if def_type == '1': ''' 这里是注册时的用户输入框 ''' username = request.POST.get('username') send_data = {'stus': 200, 'data': '可以使用'} if len(username) < 2: send_data = {'stus': 201, 'data': '你这名字也太短了吧'} if len(username) > 8: send_data = {'stus': 200, 'data': '……我感觉你这名字有点长,不过算了,你随意'} if ' ' in username: send_data = {'stus': 201, 'data': '用户名请不要用空格'} if models.User.objects.filter(username=username).first(): send_data = {'stus': 201, 'data': '有这人了'} send_data = json.dumps(send_data) return HttpResponse(send_data) if def_type == '2': ''' 这个是发送手机的验证码 ''' phone_num = request.POST.get('phone_num') yzm_num, _ = get_yanzhengma() # 生成随机的4位纯数字验证码 send_txy_yzm(yzm_num, phone_num) # 通过腾讯云发送验证码 send_data = {'stus': 200, 'yzm_num': yzm_num} print('手机验证码是', yzm_num) send_data = json.dumps(send_data) return HttpResponse(send_data) if def_type == '3': ''' 这个是刷新用户注册时的图片验证1码 ''' yzm_str, yzm_img = get_yanzhengma() yzm_img = get_pil_base64(yzm_img) # 将图片转为二进制 yzm_img = yzm_img.decode() send_data = { 'stus': 200, 'yzm_img_2': yzm_img, 'yzm_img_str': yzm_str } send_data = json.dumps(send_data) return HttpResponse(send_data) if request.method == 'GET': yzm_str, yzm_img = get_yanzhengma() d1['yzm_str'] = yzm_str d1['yzm_img'] = yzm_img d1['yzm_img_2'] = get_pil_base64(yzm_img) return render(request, 'login_he.html', d1) if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') password2 = request.POST.get('password2') print(password, '++++', password2) # hash密码在后面 user_ico = request.POST.get('user_ico') user_type = request.POST.get('user_type') user_phone = request.POST.get('phone') files = request.FILES print('views-->reg') # TODO if not username: return render(request, 'error.html', {'error_msg': '用户名长度太短了'}) if len(username) < 3: return render(request, 'error.html', {'error_msg': '用户名长度太短了'}) if ' ' in username: return render(request, 'error.html', {'error_msg': '用户名不能有空格'}) if len(password) < 3: return render(request, 'error.html', {'error_msg': '密码长度太短了'}) if password != password2: return render(request, 'error.html', {'error_msg': '两次的密码不同啊'}) # # # # 我应该在这里吧密码hash一下 password = funcs_1.get_pwd_hash(password) print(username) user_obj = models.User.objects.filter(username=username) if user_obj: return redirect('/reg') # if not user_type.isdecimal(): # return redirect('/reg') # # user_type = int(user_type) if files: django_file = File( files.get('user_ico')) # 先读取文件, 然后将其转换为orm可识别的对象 # # 保存的文件名就会是 用户名+_X_+文件名 django_file.name = username + '_X_' + django_file.name user_obj = models.User(username=username, password=password, user_ico=django_file) else: # 如果用户不上传头像,那就直接使用默认的头像 user_obj = models.User(username=username, password=password) user_obj.save() ret_obj1 = redirect('/index/') request.session['username'] = username return ret_obj1
import django django.setup() from app01 import models # 一对多 # # user_obj = models.User.objects.filter(username='******').first() # print(user_obj.username) # pscly # models.Files.objects.create(file_name='a1', user) # file_obj = models.Files.objects.all() # # file_obj_count = file_obj.count() # file_obj_count_ye, file_obj_count_yu = (file_obj_count // 6) + 1, file_obj_count % 6 # print(file_obj_count_ye, file_obj_count_yu) # file_obj_count_ye, file_obj_count_yu = divmod(file_obj_count, 6) # print(file_obj_count_ye, file_obj_count_yu) # objs = models.Files.objects.order_by('-file_date').filter(user=35) # for i in objs: # print(i.file_name) # 多对多 for i in range(5): username = input(":>>") password = '******' user_obj = models.User(username=username, password=password) user_obj.save()
#!/usr/bin/env python3 import os if __name__ == '__main__': os.environ.setdefault("DJANGO_SETTINGS_MODULE", "about_session.settings") import django django.setup() from app01 import models # 批量导入500条数据 data = [ models.User(username="******".format(i), password="******") for i in range(1, 501) ] models.User.objects.bulk_create(data)
#!/usr/bin/env python3 """ 批量向数据库插入用户 """ import os if __name__ == '__main__': os.environ.setdefault("DJANGO_SETTINGS_MODULE", "about_fenye.settings") import django django.setup() from app01 import models # 低效写法 # for i in range(500): # models.User.objects.create(name="user{}".format(i)) # 高效写法 data = [models.User(name="user{}".format(i)) for i in range(403)] models.User.objects.bulk_create(data)