def register(request): registered = False if request.method == "POST": user_form = UserForm(request.POST) details_form = UserInfoForm(request.POST) if user_form.is_valid() and details_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() details = details_form.save(commit=False) details.user = user details.save() registered = True else: print(user_form.errors, details_form.errors) else: user_form = UserForm() details_form = UserInfoForm() return render( request, 'register.html', { 'user_form': user_form, 'details_form': details_form, 'registered': registered })
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})