What is content negotiation in Django?

Md Sadiqul Islam
1 min readApr 8, 2023

--

Content negotiation is a process in Django where the server tries to serve the best available response format based on the client’s preferences. It is a way to dynamically determine the most appropriate representation of a resource based on the HTTP request headers sent by the client.

In Django, content negotiation can be done using the built-in django.shortcuts.render function or the django.views.generic.View class. By default, Django supports four content types: HTML, JSON, XML, and plain text.

When a client sends a request to a Django server, it includes an Accept header in the HTTP request, which specifies the content types it can handle. The server then checks this header and returns the most appropriate response based on the available content types.

For example, if the client specifies Accept: application/json, the server will return a JSON response if available. If the client does not specify an Accept header, the server will return an HTML response by default.

Django also provides several ways to customize content negotiation, such as defining your own content types, using content negotiation with class-based views, and using content negotiation with APIs.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response