> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Set up Algolia for Django

> Integrate Algolia search into your Django project.

export const Index = () => <Tooltip tip="An Algolia index is a searchable dataset that consists of records and configuration settings. These settings define how the records are searched and ranked.">
    index
  </Tooltip>;

This package lets you integrate the Algolia Search API into your [Django](https://www.djangoproject.com/) project. It's based on the [algoliasearch-client-python](https://github.com/algolia/algoliasearch-client-python) package.

For an example, see [`algoliasearch-django-example`](https://github.com/algolia/algoliasearch-django-example).

* Compatible with **Python 3.8** or later.
* Supports **Django 4 and 5**.

## Install

```sh Command line icon=square-terminal theme={"system"}
pip install --upgrade 'algoliasearch-django>=4.0,<5.0'
```

## Configure Django settings for Algolia

In your Django settings, add `algoliasearch_django` to `INSTALLED_APPS` and add these two settings:

```python Python icon=code theme={"system"}
ALGOLIA = {"APPLICATION_ID": "ALGOLIA_APPLICATION_ID", "API_KEY": "ALGOLIA_API_KEY"}
```

You can also add these optional settings:

* `INDEX_PREFIX`

  A prefix for all indices.
  Use it to separate different indices, like `site1_Products` and `site2_Products`.

* `INDEX_SUFFIX`

  A suffix for all indices.
  Use it to differentiate development and production environments,
  such as `Location_dev` and `Location_prod`.

* `AUTO_INDEXING`

  If true (default), models are automatically synced with Algolia.

* `RAISE_EXCEPTIONS`

  Raise exceptions for network errors instead of logging them (defaults to the value of `settings.DEBUG`).

## Define your index

Create an `index.py` in each app with the models you want to index.
Inside this file, call `algoliasearch.register()` for each model:

```python Python icon=code theme={"system"}
# index.py

import algoliasearch_django as algoliasearch

from .models import YourModel

algoliasearch.register(YourModel)
```

Algolia indexes all model fields by default.
You can configure the <Index /> by creating a subclass of `AlgoliaIndex` and using the `register` decorator:

```python Python icon=code theme={"system"}
# index.py

from algoliasearch_django import AlgoliaIndex
from algoliasearch_django.decorators import register

from .models import YourModel


@register(YourModel)
class YourModelIndex(AlgoliaIndex):
    fields = ("name", "date")
    geo_field = "location"
    settings = {"searchableAttributes": ["name"]}
    index_name = "my_index"
```

## Connection errors

If you get one of these errors: `Impossible to connect`, `Unable to connect`,
or `Unreachable hosts`:

* Ensure you're using the correct application ID and API key.
  You can find your credentials on your [Algolia dashboard](https://dashboard.algolia.com/account/api-keys/all).
* Check for recent changes in your code.
* Check the status of your data center provider.

<Info>
  If you're using Firebase, you can only access Algolia from a paid Firebase tier.
</Info>

If you can't solve the problem yourself,
contact the [Algolia support](https://support.algolia.com/hc/en-us/requests/new) team and provide them with the following information:

* The name of your [framework integration](/doc/framework-integration) (Django) and its version number
* A code snippet to reproduce the issue
* Error message or stack trace (if applicable)
* The name of the Algolia index that's causing problems
* The exact [UTC time](https://wikipedia.org/wiki/Coordinated_Universal_Time) of the event
* If you can't connect to the Algolia API from your browser, send the output from [`community.algolia.com/diag`](https://community.algolia.com/diag/).
* If you can't connect to the Algolia API from your servers, send the output from the following command (run on any affected server):

  ```sh Command line icon=square-terminal theme={"system"}
  curl -sL https://algolia.com/downloads/diag.sh > ./diag.sh && sudo ./diag.sh ALGOLIA_APPLICATION_ID
  ```

<Check>
  Replace `ALGOLIA_APPLICATION_ID` with *your* Algolia application ID.
</Check>

## Handle indexing errors

Any `Record at the position XX objectID=XX is too big` errors during indexing are because you exceeded the size limit for records.
Trim oversized records to fit the [record size limit](https://support.algolia.com/hc/en-us/articles/4406981897617-Is-there-a-size-limit-for-my-index-records-), then retry indexing.
