Dash forms with Django ( djnago-plotly-dash)

Hello everyone,

I have created a form using dash plotly, but I’m not sure on how i can send it to a django view to save in a database.

How can i send a post http request to a django view.

Best regards,
^^

If you’re using django-plotly-dash, then every callback is essentially ‘inside’ the server like any django view.

Inside an extended callback you can access a number of objects, including the Django request and user objects associated with the callback.

Hi, delsim, Currently, django-plotly-dash does not support duplicate callbacks or the latest version of Dash. Are there any plans to update this in the future?

Hello,

From the code above how can i send a post request back to the view with the form content to save it?


@dash_form_project.expanded_callback(
    dash.dependencies.Output("submit_result", "children"),
    [
        dash.dependencies.Input("submit_id", "n_clicks"),
        dash.dependencies.State("form_content", "children"),
        dash.dependencies.State("analysis_type_selector", "value"),
    ],
    prevent_initial_call=True,
)
def save_config(*args, **kwargs):
    analysis_type = analysis_types[int(args[2])]
    form = getattr(mm_schema, analysis_type["label"])
    request = kwargs["request"]
    form_content = args[1]
    if validate_form(form_content):
        form_data = {}
        for i in form_content:
            if i["props"]["id"] != "submit_id":
                form_data[i["props"]["id"]] = i["props"]["value"]
        form_instance = form(**form_data)
        return [dmc.Alert("Failed to save configuration", color="green")]
    else:
        return [dmc.Alert("Please fill all required fields", color="red")]

Once you have your form_instance object you can then perform normal Django actions with it, such as saving it.

eg something like

if form_instance.is_valid():
    my_obj = form_instance.save(commit=False)
    # anything else needed to manipulate my_obj
    my_obj.save()

This should work if directly inserted into your extended callback code.