I have a function which I want to pass two optional parameters. I have read through but somehow I am not able to make it work though.
What am I missing here?
I have something like this
def json_response_message(status, message, option_key, option_value):
data = {
'status': status,
'message': message,
}
if option_key:
data[option_key] = option_value
return JsonResponse(data)
the last two parameters I want it to be optional.
I have seen there it can be done by doing
def json_response_message(status, message, option_key='', option_value=''):
but I don't really want to do it this way and saw that there is a way to pass *args and **kwargs but it couldn't get it to work though.
I am just stuck at putting the optional parameters but not sure how to call and use them. I read through some posts and its' easily be done and call by using a for loop but somehow it just didn't work for me
def json_response_message(status, message, *args, **kwargs):
data = {
'status': status,
'message': message,
}
return JsonResponse(data)
I want add extra parameters into my data return such as...
user = {
'facebook': 'fb',
'instagram': 'ig'
}
return json_response_message(True, 'found', 'user', user)
{'status': status, 'message': message, 'user': { 'facebook': 'fb', 'instagram': 'ig'} }or{'status': status, 'message': message, 'facebook': 'fb', 'instagram': 'ig'}(i.e. you want to append optional parameter to response as dictionary or do you want to return flat structure, but add multiple key-value pairs through optional function args)?