gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
how return simple 200 django response
- User
how return simple 200 django response
- Assistant
In Django, if you want to return a simple HTTP 200 OK response, you can use the
HttpResponseclass fromdjango.http. Here is a basic example of how to do this in a view:from django.http import HttpResponse def my_view(request): return HttpResponse("OK", status=200)Here's a breakdown of the code:
HttpResponse: This class is used to create an HTTP response."OK": This is the content of the response body. You can put any string message or HTML content here.status=200: This is optional since 200 is the default status code forHttpResponse, but it's explicitly stated here for clarity.
You can place this function in one of your view modules, and then connect it to a URL pattern in your
urls.pyfile to make it accessible via HTTP requests.If you need the response to include specific headers or other details, you can add them like so:
def my_view(request): response = HttpResponse("OK", status=200) response['Custom-Header'] = 'CustomValue' return response