def regist(request): if request.method == 'GET': return render(request, 'user/regist.html') else: # post # user = UserProfile() # user.username = request.POST.get('username') # user.password = request.POST.get('password') # user.phone = request.POST.get('phone') # user.photo = request.FILES.get('photo') # 创建UserForm的实例对象 # 要求:表单中指定的字段名必须和model模型中字段名保持一致 userForm = UserForm(request.POST) # 保存之前要验证数据 if userForm.is_valid(): # 验证通过 userForm.save() return redirect('/art/') else: print('验证出错:', userForm.errors.as_json()) # form.errors.as_json() 返回是 json字符串 # 格式: {'字段名1':[{'message':'xx', code:'required'}], # '字段名2':[{'message':'xx', code:'required'}]} return render(request, 'user/regist.html', {'errors': loads(userForm.errors.as_json())})
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) user_profile_data_form = UserProfileDataForm(data=request.POST) if user_form.is_valid and user_profile_data_form.is_valid: user = user_form.save() user.set_password(user.password) #Hash the password user.save() user_profile = user_profile_data_form.save(commit=False) user_profile.user = user # Set the foriegn key # Set the profile picture to the UserProfileData profile_pic field if 'profile_imgs' in request.FILES: user_profile.user_profile_pic = request.FILES['profile_imgs'] user_profile.save() registered = True else: print(user_form.errors, user_profile_data_form.errors) else: user_form = UserForm() user_profile_data_form = UserProfileDataForm() return render( request, 'userapp/register.html', { 'user_form': user_form, 'user_profile_data_form': user_profile_data_form, 'registered': registered })
def post(self, request): password = request.POST.get('password', '') telephone = request.POST.get('telephone', '') action = request.POST.get('action', '') # if not (password or telephone or action): # msg = message(msg='请求信息不全!') # return JsonResponse(msg) # 登陆 if action == 'login': password = make_security(password.encode('utf8')) user = User.objects.filter(telephone=telephone, password=password).first() if user: user_json = user.tojson_except_evaluation() user_json['msg'] = "登录成功!" user_json['status'] = 'success' user_json['access_token'] = user.access_token return JsonResponse(user_json) else: msg = message(msg='账户或密码错误!') return JsonResponse(msg) # 注册 elif action == 'register': password = make_security(password.encode('utf8')) access_token = make_security((telephone + password).encode('utf8')) user = User(name=telephone[:2] + telephone[9:], password=password, telephone=telephone, access_token=access_token) try: user.save() except IntegrityError as ie: print(ie) msg = message(msg='手机重复!') return JsonResponse(msg) msg = message(msg='注册成功!', status='success') return JsonResponse(msg) # 修改 elif action == 'alter': access_token = request.META.get(ACCESS_TOKEN, '') if access_token: user = User.objects.filter(access_token=access_token).first() name = request.POST.get('name', '') if name: user.name = name user.save() else: user_form = UserForm(request.POST, request.FILES, instance=user) if user_form.is_valid(): user_form.save() msg = message(msg='修改成功!', status='success') return JsonResponse(msg) msg = message(msg='请求信息不全!') return JsonResponse(msg)
def modifyuser_view_new(request, user_id): print("In Modifyuser_view, method = ", request.method) print("user_id =", user_id) if request.method == 'POST': print("In modifyuser_view, [Method = POST]") user_form = UserForm(request.POST) userprofile_form = UserProfileForm(request.POST) print("userprofile_form.is_bound = ", userprofile_form.is_bound) print("userprofile_form.is_valid = ", userprofile_form.is_valid()) print("User Form: ", user_form.is_valid()) if user_form.is_valid() and user_form.is_valid(): user_form_obj = user_form.save(commit=False) user_db_obj = User.objects.get(pk=user_id) user_db_obj.first_name = user_form_obj.first_name user_db_obj.last_name = user_form_obj.last_name user_db_obj.email = user_form_obj.email print("user_db_obj.id = ", user_db_obj.id) print("user_db_obj.username = "******"user_db_obj.first_name = ", user_db_obj.first_name) user_db_obj.save() print("saved user") print("Now saving user_profile") up_db_obj = UserProfile.objects.get(user=user_db_obj) up_form_obj = userprofile_form.save(commit=False) up_db_obj.phone = up_form_obj.phone up_db_obj.save() print("Redirecting to /adminapp/usermanagement/...") return HttpResponseRedirect('/adminapp/usermanagement/') else: print("user_form is invalid") else: print( "Instantiating the User with selected_user from before user_id= ", user_id) mod_user = User.objects.get(pk=user_id) print("mod_user.username = "******"mod_up.user.username = "******"userprofile_form =", userprofile_form) print("userprofile_form.is_bound = ", userprofile_form.is_bound) print("userprofile_form.is_valid = ", userprofile_form.is_valid()) print("Rendering it now: ") return render(request, 'usermodify.html', { 'user_form': user_form, 'userprofile_form': userprofile_form })
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES["profile_pic"] profile.save() registered=True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileForm() return render(request, "userapp/registration.html",{"user_form": user_form, "profile_form": profile_form, "registered": registered})
def register(req): registered = False if req.method == "POST": user_form = UserForm(data=req.POST) profile_form = UserProfileInfoForm(data=req.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) #method for hash user.save() profile = profile_form.save(commit=False) profile.user = user if 'profile_pic' in req.FILES: #files get pic pdfs profile.profile_pic = req.FILES['profile_pic'] profile.save() registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileInfoForm() return render( req, 'userapp/registration3.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def users_views(request): context = {} if request.POST: form = UserForm(request.POST) if form.is_valid(): user = form.save() mobile_number = form.cleaned_data.get('mobile_number') raw_password = form.cleaned_data.get('password1') is_admin = form.cleaned_data.get('is_admin') is_staff = form.cleaned_data.get('is_staff') if is_admin == True: group = Group.objects.get(name='admin') user.groups.add(group) elif is_staff == True: group = Group.objects.get(name='user') user.groups.add(group) # account = authenticate(mobile_number = mobile_number, password = raw_password) # login(request,account) messages.success(request, 'New user created successfully!') return redirect('/users') else: # context['users_form'] = form messages.error(request, form.errors) return redirect('/users') else: form = UserForm() context['users_form'] = form return render(request, 'userapp/users.html', context)
def regist(request): if request.method == 'POST': form = UserForm(request.POST) # 自动将POST中的数据提取出来 # 验证是否存在错误 if form.is_valid(): # 按照文件路径分隔出文件名 # cleaned_data 是去除所有字段两边空白后的数据字典 photo = form.cleaned_data.get('photo') print(photo) if photo: file_name = os.path.split(photo)[-1] # 将临时的文件,变成持久文件 full_photo = os.path.join(settings.BASE_DIR, 'static/'+photo) to_full_photo = os.path.join(settings.BASE_DIR, f'static/images/{file_name}') os.rename(full_photo, to_full_photo) form.photo = f'images/{file_name}' # 删除tmp临时文件目录 tmp_dir = os.path.join(settings.BASE_DIR, 'static/images/tmp') os.system(f'rm -rf {tmp_dir}') form.save() # 保存数据 return redirect('/admin/') else: # 将errors的html字符串转成json字符串 errors = json.loads(form.errors.as_json()) print(errors) return render(request, 'user/regist.html', locals())
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES['profile_pic'] registered = True profile.save() else: print(user_form.erros,profile_form.errors) else: user_form = UserForm() profile_form = UserProfileInfoForm() return render(request,'userapp/registration.html', { 'user_form':user_form, 'profile_form':profile_form, 'registered':registered, })
def loginView(request): form = UserForm() if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): print("valid") Email = request.POST['Email'] Password = request.POST['Password'] userType = request.POST['utype'] if userType == "admin": print("in admin") admin = AdminModel.objects.raw( 'select * from userapp_AdminModel') for a in admin: if a.Email == Email and a.Password == Password: request.session['Email'] = Email request.session['utype'] = "admin" return redirect('/Foodapp/foods') elif userType == "user": allusers = UserModel.objects.raw( 'select * from userapp_UserModel') for u in allusers: #if u.email==email and u.password==password and u.utype==userType: if u.Email == Email and u.Password == Password: request.session['Email'] = Email request.session['utype'] = "user" print(request.session['utype']) return redirect('/Foodapp/foods') return render(request, 'userapp/login.html', { 'form': form, 'error': 'Bad credentials' }) else: return render(request, 'userapp/login.html', { 'form': form, 'error': 'Bad credentials' }) #else: #print("Hi...") return render(request, 'userapp/login.html', {'form': form})
def register(request): if request.method == 'POST': print request.POST form = UserForm(request.POST) if form.is_valid(): print form.save() return HttpResponseRedirect('/thanks/') else: form = UserForm() return render(request, 'register.html', {'form': form})
def addUserView(request): print('In AddUser') form = UserForm() if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): form.save() return redirect('/userapp/users') return render(request, 'userapp/register.html', {'form': form})
def updateUserByIdView(request, id): print("In updateUserByIdView") foundUser = UserModel.objects.get(id=id) form = UserForm(instance=foundUser) if request.method == 'POST': form = UserForm(request.POST, instance=foundUser) if form.is_valid(): form.save() return redirect('/userapp/users') return render(request, 'userapp/updateuser.html', {'form': form})
def employee_add(request): context = {} if request.method == 'POST': user_form = UserForm(request, POST) context['user_form'] = user_form if user_form is valid(): user_form.save() return HttpResponseRedirect(reverse('employee_list')) else: return render(request, 'userapp/add.html', {"user_form": user_form}) else: user_form = UserForm() return render(request, 'userapp/add.html', {"user_form": user_form})
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() # encrpt password user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user # This one to one relationship if 'profile_picture' in request.FILES: print('found it') profile.profile_picture = request.FILES['profile_picture'] print(request.FILES) profile.save() registered = True else: # Data enter is invaild print(user_form.errors, profile_form.errors) else: # This is not a post method user_form = UserForm() profile_form = UserProfileInfoForm() forms = { 'registerd': registered, 'user_form': user_form, 'profile_form': profile_form } return render(request, 'userapp/register.html', context=forms)