Added a chapter for more complicated data (#5878)

This commit is contained in:
Michael 2023-11-08 11:11:57 +01:00 committed by GitHub
parent 8c9bc4d1af
commit e674ca7437
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -254,3 +254,61 @@ that does the POST request, handles errors and the csrftoken.
!!! tip "Give it a try" !!! tip "Give it a try"
change the values in the fields and push Save. You will see the values change the values in the fields and push Save. You will see the values
in the InvenTree log. in the InvenTree log.
#### If things are getting more complicated
In the example above we code all parameters into the URL. This is easy and OK
if you transfer just a few values. But the method has at least two disadvantages:
* When you have more parameters, things will get messy.
* When you have free text input fields, the user might enter characters that are not allowed in URL.
For those cases it is better to pack the data into a json container and transfer
this in the body of the request message. The changes are simple. Lets start with
the javascript:
```html
{% raw %}
<script>
async function example_select(){
const layernumber = parseInt(document.getElementById("layer_number").value)
const size = document.getElementById("string").value
const cmd_url="{% url 'plugin:examplepanel:transfer' %}";
data = {
layer_number: layer_number,
size: size
}
response = inventreeFormDataUpload(url=cmd_url, data=JSON.stringify(data))
}
</script>
{% endraw %}
```
Here we create a json container (data). The function stringify converts this to the
proper string format for transfer. That's all. The function inventreeFormDataUpload
does the rest of the work.
The python code in the plugin also needs minor changes:
```python
from django.conf.urls import url
import json
...
def setup_urls(self):
return [
url(r'example(?:\.(?P<format>json))?$', self.do_something, name='transfer'),
]
# Define the function that will be called.
def do_something(self, request):
data=json.loads(request.body)
print('Data received:', data)
```
The URL and the called function have no parameter names any longer. All data is in the
request message and can be extracted from this using json.loads. If more data is needed
just add it to the json container. No further changes are needed. It's really simple :-)