简体   繁体   English

如何将文本框中输入的值从 django 以文本格式传递给烧瓶?

[英]How to pass value entered in textbox to flask in text format from django?

I am a beginner in django and flask.我是 Django 和烧瓶的初学者。 I want to pass the value entered in textbox from django to flask.我想将在文本框中输入的值从 django 传递到烧瓶。

views.py视图.py

from django.shortcuts import render
import requests

# Create your views here.

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

html file html文件

<!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....在烧瓶中,我应该得到在 django 文本框中输入的值....

change form tag in html as:将 html 中的表单标签更改为:

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

change urlpatterns in urls.py as:将 urls.py 中的 urlpatterns 更改为:

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

add the following in your views.py:在您的 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.这会将变量数据传递给您的 index.html 现在您可以在任何带有 jinja 的模板中使用它。

First have a look at Django forms https://docs.djangoproject.com/en/3.1/topics/forms/#building-a-form .首先看看 Django 表单https://docs.djangoproject.com/en/3.1/topics/forms/#building-a-form You'll then do not have to parse anything from you request, Django will to that for you.然后,您不必从您的请求中解析任何内容,Django 会为您解析。

Secondly have a look at https://flask-restful.readthedocs.io/en/latest/ with allows you to build a Webinterface.其次看看https://flask-restful.readthedocs.io/en/latest/允许你构建一个 Web 界面。

Then you can use a Http POST request to send your data from Django to Flask, see https://stackoverflow.com/a/11324941/876847然后您可以使用 Http POST 请求将数据从 Django 发送到 Flask,参见https://stackoverflow.com/a/11324941/876847

This should loosely couple your apps so that they can run seperately这应该松散地耦合您的应用程序,以便它们可以单独运行

I have found a solution for the question, but I want to know whether there is any other way to pass data.我已经找到了该问题的解决方案,但我想知道是否还有其他方法可以传递数据。

views.py视图.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 html文件

<!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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM