Skip to content

How to customize the Response? #445

Answered by dlpuzcl
dlpuzcl asked this question in Q&A
May 11, 2022 · 3 comments · 3 replies
Discussion options

You must be logged in to vote

I implemented this function by override create_response(), thanks for SmileyChris, and vitalik help.

class FuNinjaAPI(NinjaAPI):
	def create_response(
			self, request: HttpRequest, data: Any, *, status: int = 200, msg: str = "success"
	) -> HttpResponse:
		std_data = {
			"code": status,
			"data": data,
			"msg": msg
		}
		content = self.renderer.render(request, std_data, response_status=status)
		content_type = "{}; charset={}".format(
			self.renderer.media_type, self.renderer.charset
		)

		return HttpResponse(content, status=status, content_type=content_type)

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@dlpuzcl
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@johnbridstrup
Comment options

@sebastian-philipp
Comment options

Answer selected by dlpuzcl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants