Home:ALL Converter>Using data returned from forms in Django

Using data returned from forms in Django

Ask Time:2020-05-21T08:51:41         Author:PBERTONE

Json Formatter

Sorry for being a little new. My goal is to use the data collected from a user form to output a graph. The user inputs an initial 'price' then submits this number, it then goes into my formulas written in python and then django should display a chart in the same HTML file. I'm currently stuck on how to use data from 'POST'.

For example, if I'd like to take the cleaned_data from 'strike1' and multiply it by 4, then display it on the webpage, how would I go about doing that?


from django.shortcuts import render
from .forms import DataForm

# Create your views here.

def data(request):

    if request.method == 'POST':
        form = DataForm(request.POST)
        if form.is_valid():

            strike1 = form.cleaned_data['strike1']
            strike2 = form.cleaned_data['strike2']
            strike3 = form.cleaned_data['strike3']
            strike4 = form.cleaned_data['strike4']
            strategy = form.cleaned_data['strategy']
            price = range(0,50)
            premium1 = form.cleaned_data['premium1']
            premium2 = form.cleaned_data['premium2']
            premium3 = form.cleaned_data['premium3']
            premium4 = form.cleaned_data['premium4']
            contracts = form.cleaned_data['contracts']

    form = DataForm()
    return render(request, 'form.html', {'form': form})


from django import forms

class DataForm(forms.Form):

    strategy = forms.ChoiceField(
            choices=[('Long Call', 'Long Call'), ('Long Put', 'Long Put'), ('Short Call', 'Short Call',),
                     ('Short Put', 'Short Put'), ('Bull Call Spread', 'Bull Call Spread'),
                     ('Bear Put Spread', 'Bear Put Spread'), ('Straddle', 'Straddle'),
                     ('Butterfly Spread', 'Butterfly Spread'),
                     ('Box Spread', 'Box Spread'), ('Iron Condor', 'Iron Condor')])
    strike1 = forms.FloatField()
    strike2 = forms.FloatField()
    strike3 = forms.FloatField()
    strike4 = forms.FloatField()

    premium1 = forms.FloatField()
    premium2 = forms.FloatField()
    premium3 = forms.FloatField()
    premium4 = forms.FloatField()

    contracts = forms.IntegerField()


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <form method="'post">

        {% csrf_token %}

        {{ form }}

        <button type="Submit">Generate</button>


Author:PBERTONE,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/61925344/using-data-returned-from-forms-in-django
ShibbySham :

I'd say the cleanest way to do it, is to build up a new \"content\" and render a new page in the POST processing section. For example:\n\nfrom django.shortcuts import render\nfrom .forms import DataForm\n\n# Create your views here.\n\ndef data(request):\n\n if request.method == 'POST':\n form = DataForm(request.POST)\n if form.is_valid():\n\n # ... do you processing as it\n\n return render(request, 'output.html', {'data': whatever}) \n\n form = DataForm()\n return render(request, 'form.html', {'form': form})\n\n\nYou could also just let it call through to the original render function, but you'll need to include whatever information you want in the context like {\"form: form} already is.",
melihuyelik :

U need to create 2 views for using data.(basicly)\nOne for form, one for your formulas. \nin urls.py\n\npath('formpage/', views.formpage, name='form_view'),\n\npath('result/', views.result, name='MY_calculator'),\n\n\nin views.py\n\ndef formpage(request):\n return render(request, '-----Template-----',)\n\ndef result(request):\n x=request.GET.get('xval')\n y=request.GET.get('yval')\n result=x+y\n return render(request, '-----Result Template----', {'result': result})\n\n\nand form action for template. (\"\" instead of)\n\n<form action=\"{% url 'MY_calculator' %}\">\n",