Is There any way to keep the input data by user after submitting form ( Django / Python / HTML )

I put the input data one by one in dictionary then use TemplateResponse in the view to return dictionary , in HTML i use {{dict}} but it is not the optimum way

Part of View

if(request.GET.get('mybtn')):

        if str(request.GET.get('tech')) == "2G":
                        dic = {}
            dic['productname'] = str(request.GET.get('productname'))
                        # After calculation get price and should be returned  
            dic['price'] = "50"

            return TemplateResponse(request,'cell.html',dic)

Part of HTML

<input style="width: 80%;"  type="text" name="productname" required value={{ productname }}> 

<label for="price">{{ price }}</label> 

however , it is expected to return the price only and keep the view of the product name which is already entered
as in the real case there are a lot of inputs and selections

1 Answers

0
007fred On

Can you try this here?

python:

dic_productname = ""
dic_price = ""

if(request.GET.get('mybtn')):

        if str(request.GET.get('tech'))=="2G":
                        dic={}
            dic_productname=str(request.GET.get('productname'))
                        # After calculation get price and should be returned  
            dic_price="50"

            return render_template('cell.html', dic_productname = dic_productname, dic_price=dic_price)

html:

<input style="width: 80%;"  type="text" name="productname" required value={{dic_productname}}> 

<label for="price">{{dic_price}}</label>