django-rest-caller
Simple django templatetag for calling an urlconf view endpoint.
Limitations
- it works only for GET methods
- it doesn't handle request without a body
- it doen't play nice with login required views (it assumes that the caller handles everything it is required to access the endpoint)
- it assumes that the endpoint returns a json
Installation
Install with pip
$ python3 -m pip install django-rest-caller
Add caller.apps.CallerConfig to INSTALLED_APPS
INSTALLED_APPS = [
...
'caller.apps.CallerConfig',
...
]
Usage
call
In your template load the templatetag
{% load caller_tags %}
and use the call tag as
{% call 'urlconf' arg1=42 arg2='X' with param1='1' param2='2' as 'object_name' %}
or
{% call 'urlconf' 42 'X' with param1='1' param2='2' as 'object_name' %}
'urlconf' arg1=42 arg2='X'this is the usual {% url %} parameters (remember: use args parameter list or kwargs parameters, not both)param1='1' param2='2'these parameters will be converted to GET querystringas 'object_name'store the called object into object_name object. It can be a string or a variable name.
so the called url is equivalent to
{% url 'urlconf' arg1=42 arg2='X' %}?param1=1¶m2=2
The call will inject the result json object into the template context, so you can
- use as context object
{% load caller_tags %}
{% call 'api:blog-list' as 'posts' %}
{% for post in posts %}
<div>
<h2>{{ post.title }}</h2>
<p>{{ post.body }}</p>
</div>
{% endfor %}
- feeding to json tag
{% load caller_tags %}
{% call 'api:blog-list' as 'posts' %}
{{ posts|json_script:"posts-data" }}
<script>
function get_json(node) {
var el = document.getElementById(node);
return JSON.parse(el.textContent || el.innerText);
}
var posts = get_json("posts-data");
console.log(posts);
</script>
json_script
This tag will backport the django >= 2.1 json_script filter,
which safely outputs a Python object as JSON, wrapped in a <script> tag, ready for use with JavaScript.
example
with
value = {'hello': 'world'}
and
{{ value|json_script:"hello-data" }}
will output
<script id="hello-data" type="application/json">{"hello": "world"}</script>
and can be retrieved with
function get_json(name) {
var el = document.getElementById(name);
return JSON.parse(el.textContent || el.innerText);
}
var data = get_json("hello-data");
console.log(data);
Changes
dev
- export utils.call in init, so can do
from caller import call
0.2.1
- fix VERSION import error
0.2.0
- extract generic code from templatetag
0.1.5
- proper url unquote arg parameters
- can use templatefilters in templatetag parameters
0.1.4
- show raised exception in called view instead of a generic one
0.1.3
- always require
as 'varname' - be able to use args or kwargs for urlconf as documented
- update documentation
0.1.1 - 0.1.2
- update documentation
0.1.0
- initial release
Metadata
Release files for django-rest-caller 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_rest_caller-0.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Release files / django_rest_caller-0.2.2-py3-none-any.whl
| Download URL | django_rest_caller-0.2.2-py3-none-any.whl |
|---|---|
| Size | 28.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a462a417c22f0776cfb6f44da5215befadd224de46d2034886e36e69d06ac845
|
|
BLAKE2b-256 checksum How to use checksums |
7df7eb3ae4d33d3b1f0adabb1549df72768f82ba42edcc8e48edc2c71a34a211
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.3
|