二手车项目(三)
buy-views.py
1.主页登录
def login(request): ''' 登录视图 ''' # request为get时,index页面 if request.method == 'GET': return render(request, 'login.html') # request为post时,login页面 else: # post获取username,password username = request.POST.get('username', '') password = request.POST.get('password', '') # print('1-------------------') # auth验证用户,密码 user = auth.authenticate(username=username, password=password) # print('2-------------------') # print(username) # print(password) # auth不为空时,自动登录 if user is not None and user.is_active: auth.login(request, user) return redirect('/') # print('3-------------') else: return render(request, 'login.html', {'message':'用户名或密码错误!'})
2.主页用户注销
def logout(request): ''' 注销视图 :param request: :return: ''' auth.logout(request) return redirect('/')
3.基本注册
def register(request): if request.method == 'GET': # 查看注册页面 return render(request,'register.html') else: # 接收用户名 new_user.username = request.POST['username'] # 判断用户名称是否存在 oldUser = Users.objects.filter(username=new_user.username) if len(oldUser) > 0 : return render(request,'register.html',{'message':'用户名已经存在'}) # 处理密码,调用django中的密码加密处理程序 new_user.password = make_password(request.POST['userpwd'],None,'pbkdf2_sha1') # 根据用户点击的按钮去往不同的页面 if 'tobuy' in request.POST: return render(request,'buyregister.html') if 'tosale' in request.POST: return render(request,'info-message.html')
3.1买家详情注册
def buyinfo(request): ''' 买家注册信息 :param request: :return: ''' print('username-----------', new_user.username) if request.method == 'POST': new_user.realname = request.POST['realname'] new_user.uidentity = request.POST['uidentity'] new_user.address = request.POST['address'] new_user.cellphone = request.POST['cellphone'] new_user.sex = request.POST['sex'] try: # 存入数据库 new_user.save() except ObjectDoesNotExist as e: return HttpResponse(e) return redirect('/') else: return redirect('/userinfo/register')
3.2卖家详情注册
def saleinfo(request): if request.method == 'POST': # 获取用户信息插入到 Users 表中,继续向new_user中增加数据 new_user.realname = request.POST['realname'] new_user.uidentity = request.POST['identity'] new_user.address = request.POST['address'] new_user.cellphone = request.POST['phone'] new_user.sex = request.POST['gender'] try: new_user.save() except ObjectDoesNotExist as e: return HttpResponse(e) # 获取车辆信息插入到 Carinfo 表中 # 获取车辆品牌 brand = Brand.objects.filter(btitle=request.POST['brands'])[0] #创建Carinfo对象,并获取前端的值赋值给该对象 car = Carinfo() car.ctitle = request.POST['model'] car.regist_date = request.POST['regist_date'] car.engineno = request.POST['engineNo'] car.mileage = request.POST['mileage'] car.maintenance_record = request.POST['isService'] car.price = request.POST['price'] car.extractprice = int(car.price)*0.02 + int(car.price) car.newprice = request.POST['newprice'] # request.FILES['图片框name'] 获取上传文件 car.picture = request.FILES['pic'] car.formalities = request.POST['formalities'] car.debt = request.POST['isDebt'] car.promise = request.POST['promise'] car.serbran = brand car.user = new_user try: car.save() except ObjectDoesNotExist as e: return HttpResponse(e) return redirect('/') else: return redirect('/userinfo/register')