diff --git a/docs/docs/extend/plugins/panel.md b/docs/docs/extend/plugins/panel.md index 51bbc016f5..babb6bc848 100644 --- a/docs/docs/extend/plugins/panel.md +++ b/docs/docs/extend/plugins/panel.md @@ -254,3 +254,61 @@ that does the POST request, handles errors and the csrftoken. !!! tip "Give it a try" change the values in the fields and push Save. You will see the values 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 %} + +{% 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(?:\.(?Pjson))?$', 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 :-) +