def register(request): registered = False if request.method == 'POST': userform = UserForm(request.POST) userprofileform = UserProfileForm(request.POST) if userform.is_valid() and userprofileform.is_valid(): user = userform.save() user.set_password(user.password) user.save() userprofile = userprofileform.save(commit=False) userprofile.user = user if 'picture' in request.FILES: userprofile.picture = request.FILES.get('picture') userprofile.save() registered = True return redirect('/watchfilm/login/') else: print userform.errors, userprofileform.errors else: userform = UserForm() userprofileform = UserProfileForm() return render( request, 'watchfilm/register.html', { 'registered': registered, 'userform': userform, 'userprofileform': userprofileform })
def register(request): # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data if request.method == 'POST': # Attempt to grab information from the form information # Note that we make use of both UserForm and UserProfileForm user_form = UserForm(data=request.POST) user_profile_form = UserProfileForm(data=request.POST) # If the two forms are valid if user_form.is_valid() and user_profile_form.is_valid(): # Save the user form data to database user = user_form.save() # Now we hash the password with the set_password method # Once hashed, we can update the user object user.set_password(user.password) user.save() # Now sort out the UserProfile instance # Since we need to set the user attribute ourselves, we set commit=False # This delays saving the model until we're ready to avoid integrity problems profile = user_profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance profile.save() # Update our variable to tell the template registration was successful registered = True # Invalid form or forms-mistakes or something else? # Print problems to the terminals # They will also be shown to the user else: print user_form.errors, user_profile_form.errors # Not a HTTP POST so we render our form using two ModelForm instances # These forms will be blank, ready for user input else: user_form = UserForm() user_profile_form = UserProfileForm() # Render the template depending on the context return render( request, 'registration.html', { 'user_form': user_form, 'user_profile_form': user_profile_form, 'registered': registered })
def register(request): # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False if request.session.test_cookie_worked(): print ">>>> TEST COOKIE WORKED!" request.session.delete_test_cookie() # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
def add_users(request): groups = Group.objects.all() departments = Depart.objects.all() if request.method == 'GET': form = UserForm() elif request.method == 'POST': form = CreateUserForm(request.POST) if form.is_valid(): print(form.clean()) else: print(form.errors) context = {'groups': groups, 'departments': departments, 'form': form} print(int(form['usergroup'].value()) == groups[0].id) return render(request, 'app01/add_user.html', context=context)
def login(req): """ 管理员登陆视图 :param req: :return: """ if req.method == 'POST': uf = UserForm(req.POST) if uf.is_valid(): # 获取表单用户密码 username = uf.cleaned_data['username'] password = uf.cleaned_data['password'] yanzhengma = req.POST['Input_verify'] if req.COOKIES.get('validate', '').upper() != yanzhengma.upper(): return HttpResponse( "<script type='text/javascript'>alert('验证码错误');window.location.href='login';</script>" ) # 获取的表单数据与数据库进行比较 admin = Administrator.objects.filter(adminAccount=username) if admin.count() == 0: return HttpResponse( "<script type='text/javascript'>alert('用户名不存在');window.location.href='login';</script>" ) if admin[0].adminPassword != password: # 比较失败,还在login return HttpResponse( "<script type='text/javascript'>alert('用户名或密码错误');window.location.href='login';</script>" ) else: # 比较成功,跳转index response = HttpResponseRedirect('/Seal/adminindex') adminid = admin[0].adminId # 将adminid写入浏览器cookies response.set_cookie('SealadminID', adminid) return response else: return HttpResponse( "<script type='text/javascript'>alert('用户名或密码不能为空!');window.location.href='login';</script>" ) else: uf = UserForm() return render_to_response('Seal/login.html', {'uf': uf})
def edit_user(request, username=None): if username != None: edituser = User.objects.get(username=username) form = UserForm(request.POST, instance=edituser) else: edituser = None form = UserForm(request.POST) data = {} data["form"] = form if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] email = form.cleaned_data['email'] if edituser == None: user = User.objects.create_user(username, email, password) user.is_staff = False user.is_active = False user.save() return HttpResponseRedirect("/users/edit/" + user.username) return render_to_response(USER_TEMPLATE + 'edit.html', data, context_instance=RequestContext(request))
def login(request): if request.session.get('is_login', None): return redirect("/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 = models.User.objects.get(name=username) if user.password == password: request.session['is_login'] = True request.session['user_id'] = user.id request.session['user_name'] = user.name return redirect('/index/') else: message = "密码不正确!" except: message = "用户不存在!" return render(request, 'login/login.html', locals()) login_form = UserForm() return render(request, 'login/login.html', locals())
def user_action(request): post_req_data = request.POST # print post_req_data data = {} user_form = UserForm(data=request.POST) register_form = StudentRegistrationForm(data=post_req_data) student_model = StudentModel(data=post_req_data) if user_form.is_valid() and register_form.is_valid(): try: if student_model.is_valid(): address_model = AddressModel(data=post_req_data) if address_model.is_valid(): try: city = City.objects.get(city_name = post_req_data.get('city', '')) assert city,'AssertError: city not found in database' address = address_model.save(commit = False) address.city_obj = city address.save() except Exception as e: logger.error('under student.view.user_action '+str(e.args)) messages.error(request,'Error when trying to create the user. Please try again.') data = {'form': register_form,'register_error':True,'value_state':post_req_data.get('state', ''),'value_city':post_req_data.get('city', ''),'mailing_addr':post_req_data.get('mailing_address', '')} return render(request, 'register.html', data) try: user = user_form.save() # print user.password user.set_password(user.password) user.email = post_req_data.get('email',None) # print user.email user.save() import uuid student = student_model.save(commit=False) student.student = user student.address = address student.uuid_key = 'eq' + str(uuid.uuid4().fields[-1])[:8] student_pass = post_req_data.get('password',None) student.gender = post_req_data.get('gender',None) p_date = post_req_data.get('d_o_b',None) if p_date: import datetime d_o_b = datetime.datetime.strptime(p_date, '%m/%d/%Y').date() student.d_o_b = d_o_b else: pass student.higher_education = post_req_data.get('higher_education',None) student.i_agree = post_req_data.get('i_agree') student.save() kawrgs = {'student_pass' : student_pass,'student_uname' : user.username,'phone_number' : student.phone_number, 'full_name' : student.full_name, 'uuid_key' : student.uuid_key} verification_mail(user = user.id,domain = request.META['HTTP_HOST'],**kawrgs) return user_actions.login_user(request) except Exception as e: logger.error('under student.view.user_action '+str(e.args)) else: data = {'form': address_model,'register_error':True,'value_state':post_req_data.get('state', ''),'value_city':post_req_data.get('city', ''),'mailing_addr':post_req_data.get('mailing_address', '')} else: data = {'form': student_model,'register_error':True , 'value_state':post_req_data.get('state', ''),'value_city':post_req_data.get('city', ''),'mailing_addr':post_req_data.get('mailing_address', '')} except Exception as e: logger.error('under student.view.user_action '+str(e.args)) else: messages.error(request,'Error when trying to create the User.') data = {'form': register_form,'register_error':True,'value_state':post_req_data.get('state', ''),'value_city':post_req_data.get('city', ''),'mailing_addr':post_req_data.get('mailing_address', '')} return render(request, 'register.html', data)