Skip to content

Commit ae5cfd6

Browse files
committed
refs #134 -- using django.contrib.messages in callbacks
1 parent c2d2f2f commit ae5cfd6

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

demo/demo/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22

3+
from django.contrib import messages
34

45
logger = logging.getLogger(__name__)
56

@@ -31,9 +32,11 @@ def callback_success_message(request):
3132
This function will be called a form post-save/create.
3233
3334
It adds a logging message
35+
3436
"""
35-
logger.info('Sucessfully recorded form :)')
36-
return True
37+
msg = 'Sucessfully recorded form :)'
38+
logger.info(msg)
39+
messages.info(request._request, msg)
3740

3841

3942
def callback_fail_message(request):
@@ -42,5 +45,6 @@ def callback_fail_message(request):
4245
4346
It adds a logging message (error)
4447
"""
45-
logger.error('Form storing has failed :(')
46-
return True
48+
msg = 'Form storing has failed :('
49+
logger.error(msg)
50+
messages.error(request._request, msg)

demo/demo/builder/templates/formidable/formidable_list.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
<div class="title">
55
<h1>May the Form be with you!</h1>
66
</div>
7+
8+
{% if messages %}
9+
<ul class="messages">
10+
{% for message in messages %}
11+
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
12+
{% endfor %}
13+
</ul>
14+
{% endif %}
15+
716
<div>
817
<table>
918
<thead>

formidable/views.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ class CallbackMixin(object):
7373
def _call_callback(self, callback):
7474
"""
7575
Tool to simply call the callback function and handle edge-cases.
76+
77+
**WARNING!** the DRF request is not inherited from django core,
78+
`HTTPRequest`, and you should not assume they'll behave the same way.
79+
80+
If you need the "true" HTTPRequest object,
81+
use ``self.request._request`` in your callback functions.
7682
"""
7783
# If there's no callback value, it's pointless to try to extract it
7884
if not callback:
@@ -83,6 +89,11 @@ def _call_callback(self, callback):
8389
if not func:
8490
return
8591
try:
92+
# WARNING! the DRF request is not inherited from django core,
93+
# `HTTPRequest`, and you should not assume they'll behave the same
94+
# way.
95+
# If you need the "true" HTTPRequest object, use
96+
# ``self.request._request``
8697
func(self.request)
8798
except Exception:
8899
logger.error(

0 commit comments

Comments
 (0)