def login_view(request): if request.method == 'POST': form = forms.LoginForm(request.POST) if form.is_valid(): user=authenticate(username=request.POST['username'],password=request.POST['password']) if user is not None: if user.is_active: login(request, user) return redirect("accounts:homepage_view") else: return HttpResponse("You not are active but logged in!...") else: form=forms.LoginForm() return render(request,'accounts/login.html', {'form': form,})
def checkout_home(request): # Get cart and order cart_obj, cart_created = Cart.objects.new_or_get(request) order_obj = None if cart_created or cart_obj.product.count() == 0: return redirect('cart:home') login_form = forms.LoginForm() guest_form = forms.GuestForm() address_form = AddressForm() billing_profile,billing_profile_created = BillingProfile.objects.new_or_get(request) billing_address_id = request.session.get('billing_address_id') shipping_address_id = request.session.get('shipping_address_id') print(billing_address_id, shipping_address_id) if billing_profile != None: order_obj, order_obj_created = Order.objects.new_or_get(billing_profile, cart_obj) if shipping_address_id: order_obj.shipping_address = Address.objects.get(id=shipping_address_id) del request.session['shipping_address_id'] if billing_address_id: order_obj.billing_address = Address.objects.get(id=billing_address_id) del request.session['billing_address_id'] order_obj.save() context = { 'object': order_obj, 'billing_profile': billing_profile, 'guest_form': guest_form, 'login_form': login_form, 'address_form':address_form, } return render(request, 'carts/checkout.html', context)
def post(self, request): res = {"status": 0} login_form = forms.LoginForm(request.POST) if login_form.is_valid(): username = request.POST.get("username", None) password = request.POST.get("password", None) user = authenticate(username=username, password=password) if user: if user.is_active: login(request, user) res['next_url'] = '/' else: res['status'] = 1 res['errmsg'] = "用户被禁用" else: res["status"] = 1 res["errmsg"] = "用户名密码错误" else: res["status"] = 1 res["errmsg"] = "用户名或密码不能为空" return JsonResponse(res, safe=True)
def post(self,request): res = {'code':0} login_form = forms.LoginForm(request.POST) if login_form.is_valid(): username = request.POST.get('username', '') password = request.POST.get('password', '') user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) res['next_url'] = '/' else: res['code'] = 1 res['errmsg'] = '用户被禁用' else: res['code'] = 1 res['errmsg'] = '用户名或密码错误' else: res['code'] = 1 res['errmsg'] = "用户名或密码不能为空" return JsonResponse(res,safe=True)
def home(request): return render( request, "todo/index.html", { "form": forms.NewListForm(), "loginform": account_forms.LoginForm(), } )
def loginView(request): # Checking If the User is already Logged In message = '' if (request.user.is_authenticated): return checkAndRedirect(request.user) if (request.method == 'POST'): form = forms.LoginForm(request.POST) if (form.is_valid()): user = authenticate(request, username=form.cleaned_data['username'], password=form.cleaned_data['password']) if (user): login(request, user) return checkAndRedirect(request.user) else: message = 'Invalid Username Or Password' context = { 'form': forms.LoginForm(), 'error_msg': message, } return render(request, 'accounts/signin.html', context)
def logax(request): print 1 if request.method == 'POST': form = forms.LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] # check for valid login: ret, user = auth(username, password) # if it is valid, log them in. if not ret['error_no']: l = login(request, user) else: ret = {'error_no': -1, 'error_text': 'form error'} else: ret = {'error_no': -2, 'error_text': 'not POST'} print "#2", ret response = HttpResponse(json.dumps(ret, indent=1)) response['Content-Type'] = 'application/json' return response
def auth(request): """ The combined authentication and registration form page """ if request.user.is_authenticated(): return http.HttpResponseRedirect(LOGIN_REDIRECT_URL) if request.method == "POST": redirect_to = request.POST.get('next', LOGIN_REDIRECT_URL) form_type = request.POST.get('user-formtype') if request.POST.get( 'user-formtype', False) else request.POST['formtype'] # Login form submission. if form_type == "login": login_form = account_forms.LoginForm(data=request.POST) if login_form.is_valid(): netloc = urlparse.urlparse(redirect_to)[1] # Heavier security check -- don't allow redirection to a different host. if netloc and netloc != request.get_host(): redirect_to = LOGIN_REDIRECT_URL # Okay, security checks complete. Log the user in. contrib_auth.login(request, login_form.get_user()) if request.session.test_cookie_worked(): request.session.delete_test_cookie() if request.POST.get('keep-login', False): request.session[settings.KEEP_LOGGED_KEY] = True return http.HttpResponse( json.dumps(dict(success=True, redirect_to=redirect_to))) else: return http.HttpResponse( json.dumps( dict(msg='The email and password don\'t match', success=False, title='Wrong ID or Password'))) # Unbound registration form to show in case of invalidation. user_form = account_forms.RegistrationForm(prefix="user") # Registration form submission. elif form_type == "registration": user_form = account_forms.RegistrationForm(data=request.POST, prefix="user") if user_form.is_valid(): registered_user = user_form.save() password = user_form.cleaned_data['password1'] email = user_form.cleaned_data['email'] if registered_user.userprofile.email_verified: token = signup_models.OneTimeLoginToken.get_token(email) direct_login_rel_url = 'email=%s&token=%s' % (email, token) dm_utils.send_welcome_note.delay(email, password, direct_login_rel_url) # After registration, login the user and redirect to account for # filling in other details. new_user = contrib_auth.authenticate(username=email, password=password) contrib_auth.login(request, new_user) return http.HttpResponse( json.dumps( dict(success=True, redirect_to=LOGIN_REDIRECT_URL))) else: error_msg = ' '.join([ str(value[0]) for _, value in user_form.errors.iteritems() ]) return http.HttpResponse( json.dumps(dict(msg=error_msg, title='Error'))) # Unbound login form to show in case of invalidation. login_form = account_forms.LoginForm(request) pass else: assert False, "should not be coming here" else: login_form = account_forms.LoginForm(request) user_form = account_forms.RegistrationForm(prefix="user") redirect_to = request.GET.get('next', LOGIN_REDIRECT_URL) context = { 'loginform': login_form, 'userform': user_form, 'next': redirect_to, 'login_page': True } return render_to_response('signup/combo.html', context, context_instance=RequestContext(request))