def registerandlogin2(request): if request.method == "POST": form = UserForm(request.POST) if form.is_valid(): print("############# success #############") print(form.cleaned_data) print(form.errors) user = request.POST.get("name") pwd = request.POST.get("pwd") print(user, pwd) user_list = User.objects.all().values("user") for name in user_list: if user == name['user']: i_error = '您输入的用户名重复' return render(request, "registerandlogin.html", locals()) else: User.objects.create(user=user, pwd=pwd) return render(request, "registerandlogin.html") else: print("########### fail #########") print(form.cleaned_data) print(form.errors) print(type(form.errors)) print(form.errors.get("email")) print(type(form.errors.get("email"))) g_error = form.errors.get("__all__") if g_error: g_error = g_error[0] return render(request, "registerandlogin.html", locals()) else: form = UserForm() return render(request, "registerandlogin.html", locals())
def reg(request): ''' 基于ajax和forms实现的注册功能 :param request: :return: ''' if request.method == "POST": print(request.POST) form = UserForm(request.POST) res = {"user": None, "err_msg": ""} if form.is_valid(): res["user"] = form.cleaned_data.get("user") print("cleaned_data", form.cleaned_data) user = form.cleaned_data.get("user") pwd = form.cleaned_data.get("pwd") email = form.cleaned_data.get("email") user = UserInfo.objects.create_user(username=user, password=pwd, email=email) else: print(form.errors) print(form.cleaned_data) res["err_msg"] = form.errors return JsonResponse(res) else: form = UserForm() return render(request, "reg.html", locals())
def reg(request): if request.is_ajax(): print(request.POST) res = {"user": None, "err_msg": None} form = UserForm(request.POST) if form.is_valid(): res["user"] = form.cleaned_data.get("user") user = form.cleaned_data.get("user") pwd = form.cleaned_data.get("pwd") email = form.cleaned_data.get("email") gender = form.cleaned_data.get("gender") UserInfo.objects.create_user(username=user, password=pwd, email=email, gender=gender) print("正确数据:", form.cleaned_data) else: res["err_msg"] = form.errors return JsonResponse(res) else: form = UserForm() return render(request, "reg.html", locals())