Performance: HttpResponse object
This affects server response time and how quickly the browser receives and starts rendering content.
from django.http import HttpResponse def view(request): lines = ['Line ' + str(i) for i in range(10000)] content = '\n'.join(lines) return HttpResponse(content)
from django.http import HttpResponse def view(request): content = '' for i in range(10000): content += 'Line ' + str(i) + '\n' return HttpResponse(content)
| Pattern | Server CPU | Memory Usage | Response Time | Verdict |
|---|---|---|---|---|
| String concatenation in loop | High | High | Slow | [X] Bad |
| List comprehension + join | Low | Low | Faster | [OK] Good |
| Read whole large file | High | High | Slow | [X] Bad |
| StreamingHttpResponse | Low | Low | Fast | [OK] Good |
HttpResponse object in Django?HttpResponse with the text "Hello, World!" in Django?from django.http import HttpResponse
def my_view(request):
return HttpResponse('Not Found', status=404)from django.http import HttpResponse
def view(request):
response = HttpResponse
response.content = 'Hello'
return responseHttpResponse. Which code snippet correctly sets the content type to application/json?content_type.content_type='application/json' correctly; others use invalid parameters or methods.Pass the test and this pattern moves unknown