or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

admin-integration.mdcore-models.mddjango-integration.mdforms-views.mdindex.mdpreference-types.mdregistries.mdrest-api.mdserialization.mdsignals.mduser-preferences.md

django-integration.mddocs/

0

# Django Integration

1

2

Django framework integration features including template context processors, template tags, and middleware support. These components provide seamless integration with Django's template system and web framework features.

3

4

## Capabilities

5

6

### Template Context Processor

7

8

Template context processor that automatically injects global preferences into template context, making them available in all templates without manual passing from views.

9

10

```python { .api }

11

def global_preferences(request):

12

"""

13

Pass the values of global preferences to template context.

14

15

Args:

16

request: Django HttpRequest object

17

18

Returns:

19

dict: Dictionary with 'global_preferences' key containing all preference values

20

"""

21

```

22

23

### Configuration

24

25

**Settings Configuration:**

26

27

```python

28

# settings.py

29

TEMPLATES = [

30

{

31

'BACKEND': 'django.template.backends.django.DjangoTemplates',

32

'DIRS': [],

33

'APP_DIRS': True,

34

'OPTIONS': {

35

'context_processors': [

36

'django.template.context_processors.debug',

37

'django.template.context_processors.request',

38

'django.contrib.auth.context_processors.auth',

39

'django.contrib.messages.context_processors.messages',

40

# Add the global preferences context processor

41

'dynamic_preferences.processors.global_preferences',

42

],

43

},

44

},

45

]

46

```

47

48

### Usage Examples

49

50

**Template Usage:**

51

52

```html

53

<!-- In any Django template -->

54

<!DOCTYPE html>

55

<html>

56

<head>

57

<title>{{ global_preferences.general__site_title }}</title>

58

</head>

59

<body>

60

{% if global_preferences.general__maintenance_mode %}

61

<div class="maintenance-banner">

62

Site is currently under maintenance

63

</div>

64

{% endif %}

65

66

<main style="background-color: {{ global_preferences.ui__background_color }};">

67

<!-- Site content -->

68

<h1>{{ global_preferences.general__site_title }}</h1>

69

<p>Welcome message: {{ global_preferences.general__welcome_message }}</p>

70

</main>

71

</body>

72

</html>

73

```

74

75

**Accessing Nested Preferences:**

76

77

```html

78

<!-- For preferences with sections -->

79

<div class="theme-{{ global_preferences.ui__theme }}">

80

<h1>{{ global_preferences.site__title }}</h1>

81

82

{% if global_preferences.features__enable_comments %}

83

<div id="comments-section">

84

<!-- Comments functionality -->

85

</div>

86

{% endif %}

87

88

<footer>

89

Contact: {{ global_preferences.contact__email }}

90

</footer>

91

</div>

92

```

93

94

**Using with Template Filters:**

95

96

```html

97

<!-- Apply Django template filters to preference values -->

98

<p>Last updated: {{ global_preferences.site__last_updated|date:"F j, Y" }}</p>

99

<p>Site description: {{ global_preferences.site__description|truncatewords:20 }}</p>

100

<p>Admin email: {{ global_preferences.contact__admin_email|urlize }}</p>

101

```

102

103

### Performance Considerations

104

105

The context processor uses the cached preference manager, so template access to preferences is efficient and doesn't result in additional database queries on each request. The preferences are loaded once and cached until they are updated.

106

107

### Import Statement

108

109

```python

110

from dynamic_preferences.processors import global_preferences

111

```

112

113

### Integration Notes

114

115

- The context processor automatically provides all global preferences to every template

116

- Preference keys use the `section__name` format in templates

117

- Values are automatically deserialized to their proper Python types

118

- Changes to preferences are immediately reflected in templates due to cache invalidation

119

- No additional view code needed - preferences are available in all templates once configured