First time asking a question, please correct me if anything is unclear or not regulated
I'm currently writing a Django rest framework endpoint and here is my question
Here I have a template to be filled with keys, assume I received a post request with body like this
{
"times": 10,
"type": "button"
}
Here is my APIView post function
def post(self,request,format = None):
if request.data["times"]:
times = request.data["times"]
type = request.data["type"]
data = request.data
data.pop('times')
if times:
for _ in range(times):
color = color_list[random.randint(0, len(color_list) - 1)]
color_code = fake.color(hue=color)
myobj = {
"name": f"{color} background button",
"children": [],
"struct_type": data["type"] if data["type"] else "button",
"func_type": data["type"] if data["type"] else "button",
"direction": data["direction"] if data["direction"] else "vertical",
"buoyancy": data["buoyancy"] if data["buoyancy"] else 0,
"parameters": data["parameters"] if data["parameters"] else {
"freeCanvas": {
"left": 20,
"top": 20,
"zIndex": 0,
"width": 54,
"height": 27
},
"group": "form",
"layouts": [
"desktop",
"tablet",
"mobile"
],
"textContent": "button",
"showRipple": True
},
"styles": data["styles"] if data["styles"] else{
"classNames": [
"button"
],
"ownStyles": {
"color": "#000000",
"background-color": f"{color_code}"
}
},
"shared": data["shared"] if data["shared"] else True
}
print(serializer.validated_data(myobj))
return Response(f"Successfully generated {times} random {type} elements")
Take I have a "type" key within the post body, the func_type and struct_type should be fine, but the direction won't be able to check if there is a key in the post data but just tells me a key error, is any way around this?
Aucun commentaire:
Enregistrer un commentaire