def superlogin(request): kind = 1 if request.session.get('is_login', None): model = request.session['model'] if model != 1001: return redirect('/superlogout/') if request.method == "POST": login_form = UserForm(request.POST) message = "请检查填写的内容!" if login_form.is_valid(): username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] try: user = models.Superuser.objects.get(userid=username) if user.password == password: request.session['is_login'] = True request.session['user_id'] = user.userid request.session['user_name'] = user.userid request.session['model'] = 1001 return redirect('/superindex/') else: message = "密码不正确!" except: message = "用户不存在!" return render(request, 'login.html', locals()) login_form = UserForm() return render(request, 'login.html', locals())
def login(request): if request.session.get('is_login', None): return redirect('app:index') if request.method == "POST": login_form = UserForm(request.POST) message = "验证码错误!" if login_form.is_valid(): username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] try: user = User.objects.get(name=username) if user.password == hash_code(password): # 哈希值和数据库内的值进行比对 request.session['is_login'] = True request.session['user_id'] = user.id request.session['user_name'] = user.name request.session['head_photo'] = user.head_photo return redirect('app:index') else: message = "密码不正确!" except: message = "用户不存在!" return render(request, 'login.html', locals()) login_form = UserForm() return render(request, 'login.html', locals())
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) worker_form = WorkerForm(data=request.POST) user_profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and worker_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() worker = worker_form.save(commit=False) worker.user = user worker.save() registered = True elif user_form.is_valid() and user_profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() user_profile = user_profile_form.save(commit=False) user_profile.user = user user_profile.save() registered = True else: print(user_form.errors, worker_form.errors) else: user_form = UserForm() worker_form = WorkerForm() user_profile_form = UserProfileForm() return render( request, 'User/registration.html', { 'user_form': user_form, 'worker_form': worker_form, 'user_profile_form': user_profile_form, 'registered': registered })
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: print('okkkkkkkkkkkkk') 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 = UserProfileInfoForm() return render(request, 'registration.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
def uprofile(request): if request.method == 'POST': uform = UserForm(data=request.POST, instance=request.user) if uform.is_valid(): uform.save() return redirect('/home/') print(request.user) uform = UserForm(data=request.POST, instance=request.user) return render(request, 'profile.html', {"uform": uform})
def profile(request): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile) if user_form.is_valid(): user_form.save() if profile_form.is_valid(): profile_form.save() return redirect('user') else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) return render(request, "user.html", { 'user_form': user_form, 'profile_form': profile_form })
def update_profile(request): '''修改个人资料以及交友资料''' user_form = UserForm(request.POST) profile_form = ProfileForm(request.POST) if user_form.is_valid() and profile_form.is_valid(): uid = request.session.get('uid') User.objects.filter(id=uid).update(**user_form.cleaned_data) Profile.objects.update_or_create(id=uid, defaults=profile_form.cleaned_data) return render_json() else: err = {} err.update(user_form.errors) err.update(profile_form.errors) # return render_json(data={'usererr': user_form.errors, # 'profile': profile_form.errors}, # code=errors.PROGILE_ERR, # ) raise errors.ProfileErr(data=err)