3

I am a beginner in django and flask. I want to pass the value entered in textbox from django to flask.

views.py

from django.shortcuts import render
import requests

# Create your views here.

def form(request):
    return render(request,'hello/index.html')

html file

<!DOCTYPE html>
<html>
<body>


  <form action="output">
    
    Enter name: <br/>
    <input type="text" name="name"> <br/>
  
    <input type="submit" ><br/>
  
</form>
  
</body>
</html>

flask

app = Flask(__name__)

@app.route('/')
def index():
     
     return "hello" 
     
if __name__ == '__main__':
    app.run(debug=True)

In the flask i should get the values entered in django textbox....

davidism
  • 121,510
  • 29
  • 395
  • 339
Vignesh V
  • 41
  • 5

2 Answers2

0

change form tag in html as:

<form method="GET" action="output">

change urlpatterns in urls.py as:

path('output', views.output ,name ='output'),

add the following in your views.py:

def output(request):
    data = request.GET['name']
    return render(request, 'hello/**any template**.html',{'data' : data})

this will pass a variable data to your index.html Now you can use that in you any of templates with jinja.

Divyessh
  • 2,540
  • 1
  • 7
  • 24
0

I have found a solution for the question, but I want to know whether there is any other way to pass data.

views.py

from django.shortcuts import render
import requests

# Create your views here.

def form(request):
    return render(request,'hello/index.html')

def output(request):
    name1=request.GET["name"]
    if not name1:
        name1=0
    response = requests.get('http://127.0.0.1:5000/'+str(name1)).json()
    #name = response.text
    return render(request, 'hello/index.html', {'out': response['out']})

html file

<!DOCTYPE html>
<html>
<body>


  <form action="output" method="GET">
    
    Enter name: <br/>
    <input type="text" name="name"> <br/>
  
    <input type="submit" ><br/>
  
</form>
<div>
  {{out}}
</div>
  
</body>
</html>

flask file

from flask import Flask,jsonify
app = Flask(__name__)
@app.route('/<name>',methods=['GET'])
def index(name):
     return jsonify({'out' : "Hello"+  " "+str(name)})
if __name__ == '__main__':
    app.run(debug=True)
Vignesh V
  • 41
  • 5
  • Let us continue this discussion in chat.(https://chat.stackoverflow.com/rooms/223338/discussion-between-divyessh-and-vignesh-v) – Divyessh Oct 20 '20 at 07:15