Skip to content

Commit

Permalink
Create add_context_processor.md
Browse files Browse the repository at this point in the history
  • Loading branch information
williln committed May 29, 2024
1 parent 361c538 commit d1b41b3
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions django/add_context_processor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Adding a custom context processor to your Django app so you can include bits of data in your template headers more easily

## 1. Add `my_app/context_processors.py`

```python
from __future__ import annotations


def my_context(request):
"""
Adds a special notice for a sale
"""
return {"notice": "All cat toys 50% off!"}

```


## 2. Add to settings

In `settings.py`:

```python
TEMPLATES = [
{
...
"OPTIONS": {
"context_processors": [
...
"my_app.context_processors.my_context",
],
},
}
]
```

## 3. Use your context value in your template:

```html
<h2>{{ notice }}</h2>
```

0 comments on commit d1b41b3

Please sign in to comment.