Caller
Project description
# 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 handle everything it is required to access the endpoint)
* it assumes that the endpoint returns a json
## Installation
Add `caller.apps.CallerConfig` to `INSTALLED_APPS`
```python
INSTALLED_APPS = [
...
'caller.apps.CallerConfig',
...
]
```
## Usage
### call
In your template load the templatetag
```html+django
{% load caller_tags %}
```
and use the `call` tag as
```html+django
{% call 'urlconf' arg1=42 arg2='X' with param1='1' param2='2' as 'object_name' %}
```
* `'urlconf' arg1=42 arg2='X'` this is the usual {% url %} parameters
* `param1='1' param2='2'` these parameters will be converted to GET querystring
* `as '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
```html+django
{% 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
```html+django
{% 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
```html+django
{% 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`](https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#json-script) filter,
which safely outputs a Python object as JSON, wrapped in a `<script>` tag, ready for use with JavaScript.
#### example
with
```python
value = {'hello': 'world'}
```
and
```html+django
{{ value|json_script:"hello-data" }}
```
will output
```html
<script id="hello-data" type="application/json">{"hello": "world"}</script>
```
and can be retrieved with
```javascript
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
### 0.1.0
* initial release
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 handle everything it is required to access the endpoint)
* it assumes that the endpoint returns a json
## Installation
Add `caller.apps.CallerConfig` to `INSTALLED_APPS`
```python
INSTALLED_APPS = [
...
'caller.apps.CallerConfig',
...
]
```
## Usage
### call
In your template load the templatetag
```html+django
{% load caller_tags %}
```
and use the `call` tag as
```html+django
{% call 'urlconf' arg1=42 arg2='X' with param1='1' param2='2' as 'object_name' %}
```
* `'urlconf' arg1=42 arg2='X'` this is the usual {% url %} parameters
* `param1='1' param2='2'` these parameters will be converted to GET querystring
* `as '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
```html+django
{% 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
```html+django
{% 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
```html+django
{% 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`](https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#json-script) filter,
which safely outputs a Python object as JSON, wrapped in a `<script>` tag, ready for use with JavaScript.
#### example
with
```python
value = {'hello': 'world'}
```
and
```html+django
{{ value|json_script:"hello-data" }}
```
will output
```html
<script id="hello-data" type="application/json">{"hello": "world"}</script>
```
and can be retrieved with
```javascript
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
### 0.1.0
* initial release
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for django_rest_caller-0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c6d2977f245a6d2f38e50f51fb560dcc4e9eca3751c8ddd95bb58fa968417ab |
|
MD5 | eb820d2d6507d5a2acd7b16e01683324 |
|
BLAKE2b-256 | 6fa1eeb6a419cdd29f2ef631e135c43efe3d4890a3256c4c4ee224f2447b17f7 |