+++
title = "Django with django-elasticsearch-dsl"
weight = 118
updated = 2023-07-07
aliases = ["/docs/django-with-django-elasticsearch-dsl", "/docs/managed-search/elasticsearch-and-opensearch/using-your-cluster/connecting/python/django/django-with-django-elasticsearch-dsl/", "/docs/using-your-cluster/connecting/python-django-dsl/"]

[extra]
weight = 51
nav_title = "Django: elasticsearch-dsl"
+++

Users of [Django/django-elasticsearch-dsl](https://github.com/sabricot/django-elasticsearch-dsl) can easily integrate
with Bonsai Elasticsearch! This library is built on top of elasticsearch-dsl which is built on top of the low level
elasticsearch-py maintained by Elastic.

Let's get started:

## Adding the libraries

You'll need to add the django-elasticsearch-dsl library to your `requirements.txt` file:

```
django-elasticsearch-dsl>=0.5.1
elasticsearch-dsl>=6.0,<6.2
```

[Full Instructions can be found here](https://django-elasticsearch-dsl.readthedocs.io/)

## Connecting to Bonsai

Bonsai requires basic authentication for all read/write requests. You'll need to configure the client so that it
includes the username and password when communicating with the cluster. We recommend adding the cluster URL to an
environment variable, `BONSAI_URL`, to avoid hard-coding your authentication credentials.

The following code is a good starter for integrating Bonsai Elasticsearch into your app:

```python
ES_URL = urlparse(os.environ.get('BONSAI_URL') or 'http://127.0.0.1:9200/')
ELASTICSEARCH_DSL={
    'default': {
        'hosts': ES_URL
    },
}
```

### Note about ports

The sample code above uses port 443, which is the default for the https:// protocol. If you're not using SSL/TLS and
want to use http:// instead, change this value to 80.