Some helper utilities for building Ajax applications without throwing out all the nicities of Django out the window (or moving wholesale to something like Django REST Framework).
A model mixin that gives an easy way to serialize a model or queryset to JSON.
Default dictionary representation of a model that can later be converted to JSON. May be overriden in a model by defining to_json
in the model.
Serializes a queryset/model into a JSON-ifable dictionary. Will paginate a queryset if necessary.
Outputs a dictionary input a JSON object. Returns an empty obj if the template variable is empty.
<script type="text/javascript">
window.onload = function() {
(function() {
var djajaxJsonData = {{ json|jsonify }};
console.log('djajaxJsonData', djajaxJsonData);
})();
}();
</script>
Outputs a dictionary of reverse lookups to urls for use in Javascript. Urls must be specified in DJAJAX_URL_REVERSE_LOOKUPS and must not need arguments.
settings.py
DJAJAX_URL_REVERSE_LOOKUPS = [
'login',
'www:index',
]
index.html
<script src="{% static 'js/djajax.js' %}"></script>
<script type="text/javascript">
window.onload = function() {
(function() {
var djajaxUrls = {% djajax_urls %};
console.log('djajaxUrls', djajaxUrls);
})();
}();
</script>
Returns a JSON response with the correct headers set.
Returns whether the request is idempotent (changes state on the server) or not.
Returns an appropriate AJAX response for an AJAX request.
Attempts to first get a value from the POST query dictionary, and if that fails, to look at the GET querystring.