---
title: '|addslashes'
description: adds backslashes before quotation marks to escape them.
date: '2026-08-02'
categories:
  - Filter
  - Coding
canonical: https://coolify.djangotemplatetagsandfilters.com/filters/addslashes/
doc_link: https://docs.djangoproject.com/en/6.0/ref/templates/builtins/#addslashes
---

# |addslashes

adds backslashes before quotation marks to escape them.

## Documentation

Adds backslashes before quotation marks to escape them.

### Variable

```django
blurb = "Where'd you get the coconuts?"
```

### Template

```django
{{ blurb|addslashes }}
```

### Result

```django
Where\'d you get the coconuts?
```

This is particularly useful when you need to include Django variables within JavaScript code. Consider the following:

### Template

```django
<button onclick="alert('{{ blurb }}')">Alert</button>
```

### Result

```django
<button onclick="alert('Where'd you get the coconuts?')">Alert</button>
```

This will result in a JavaScript bug as the apostrophe in `Where'd` will close the JavaScript string. Using `addslashes` will fix that:

```django
<button onclick="alert('{{ blurb|addslashes }}')">Alert</button>
```

### Result

```django
<button onclick="alert('Where\'d you get the coconuts?')">Alert</button>
```

Notice that the apostrophe in `Where'd` is now escaped.

## Commentary

If you are writing raw SQL queries, do **not** use `addslashes` to escape single quotes. Use [parameters](https://docs.djangoproject.com/en/6.0/topics/db/sql/#passing-parameters-into-raw) instead.
