def register(request): user = request.user if user.is_authenticated: logout(request) return redirect(to='infomanage') if request.method == 'POST': form = UserInfoForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] user_exist = User.objects.filter(username=username) if user_exist: messages.add_message(request, messages.ERROR, "用户名已存在,请更换!") return redirect(to='register') else: password = form.cleaned_data['password'] User.objects.create_user(username=username, password=password) try: acc = User.objects.get(username=username) user = UserInfo() user.account = acc user.sex = form.cleaned_data['sex'] user.name = form.cleaned_data['username'] user.job = form.cleaned_data['job'] num = UserInfo.objects.order_by( 'job_num').reverse().all()[0].job_num user.job_num = num + 1 user.save() str = "注冊成功!" messages.add_message(request, messages.ERROR, str) except: print('error') pass return redirect(to='login') elif request.method == 'GET': form = UserInfoForm return render(request, 'register.html', {'form': form})
def register(request): if request.method == 'POST': form = UserInfoForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] user_exist = User.objects.filter(username=username) if user_exist: messages.add_message(request, messages.ERROR, "用户名已存在,请更换!") return redirect(to='register') else: user = UserInfo() user.username = form.cleaned_data['username'] user.password = form.cleaned_data['password'] user.sex = form.cleaned_data['sex'] user.job = form.cleaned_data['job'] user.job_num = form.cleaned_data['job_num'] user.save() login(request, user) return redirect(to='infomanage') elif request.method == 'GET': form = UserInfoForm return render(request, 'register.html', {'form': form})