【问题标题】:Changing values using POST method in FLASK?在 FLASK 中使用 POST 方法更改值?
【发布时间】:2016-10-21 17:18:12
【问题描述】:

我是 StackOverflow 和 Python 的新手。我正在尝试制作一个基本应用程序,其中有我的

标签、我的烧瓶应用程序和我的 .txt。我正在尝试更改值,但它不起作用,或者我不知道为什么它不起作用。有谁能帮帮我吗?

Python 烧瓶:

from flask import Flask,render_template,flash,request,redirect
import os
app = Flask(__name__)
from lines import get_line

@app.route('/', methods=['POST'])
def change_line():
    search_line= "this"
    try:
        for line in this.input(os.path.join(APP_STATIC, u'line.txt'),inplace=1):

            if search_line in line:

                    x = line.replace(search_line,search_line + "\n" + request.form.get(u'this'))

                    print (x)
            else:

                print (x)

    except BaseException as e:
        print e

    return render_template('line.html')


@app.route('/')
def showLine():
    line = get_line()
    return render_template('line.html', line=line)


if __name__ == '__main__':
    app.run()

HTML:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Change the value of this line</title>
</head>
<body>
    <form method="post" name="test">
                            <h4>Chaging Values with POST Method</h4>
                            <div class="col-sm-9">
                            <label class="col-sm-3 col-sm-3 control-label">I want to change : </label>
                            <input type="text" class="form-control" name="address" value="{{ line }}">
                            </div>

                            <input type="submit" value="Save Changes!">


    </form>

我的 .txt 文件:

我想改变这个。

它正在返回“this”的值。

基本上,当我运行我的应用程序时,它会显示“this”,当我尝试编辑“Hello”时它会返回一个错误:

  • 在http://127.0.0.1:5000/ 上运行(按 CTRL+C 退出)行 成立! : 这个。 127.0.0.1 - - [21/Oct/2016 13:09:07] “GET / HTTP/1.1”200 - 未定义全局名称“this” 127.0.0.1 - - [21/Oct/2016 13:09:30] “POST / HTTP/1.1”200 -

我的输出: Please click here to see my output

很抱歉,如果这是一个愚蠢的问题,或者之前已经回答过这个问题,但我一直在寻找答案,但我对此一无所知,我尝试了不同的代码,但没有工作,一直在看 youtube 视频和一切。

如果有人知道解决方案对我真的很有帮助。

用于学习和 Python 学校。谢谢!

使用 Python 2.7

编辑

我使用以下建议更新了代码,但它还没有工作。 它不会用替换覆盖我的“this”。

from flask import Flask,render_template,flash,request,redirect
import os
app = Flask(__name__)
from lines import get_line
import fileinput

@app.route('/', methods=['POST'])
def change_line():
    search_line= "this"
    try:
        for line in fileinput.input(os.path.join(APP_STATIC, u'line.txt'),inplace=1):

            if search_line in line:

                    x = line.replace(search_line,search_line + "\n" + request.form.get(u'asd'))

                    print (x)
            else:

                print (x)

    except BaseException as e:
        print e

    return render_template('line.html')


@app.route('/')
def showLine():
    line = get_line()
    return render_template('line.html', line=line)


if __name__ == '__main__':
    app.run(debug=True)

【问题讨论】:

  • 你希望this.input 做什么?
  • 他想要fileinput.input 你可以从上下文中看出...:/

标签: python python-2.7 flask


【解决方案1】:
this.input(os.path.join(APP_STATIC, u'line.txt'),inplace=1):

应该是

fileinput.input(os.path.join(APP_STATIC, u'line.txt'),inplace=1):

(您还需要import fileinput)

你需要改变它,因为this.input 对 python 没有任何意义......它不知道 this 是什么......

全局名称“this”未定义

顺便说一句,您应该在开发时使用app.run(debug=True) 运行

【讨论】:

  • 我试试这个!感谢您的宝贵时间,如果这是一个愚蠢的问题,我们深表歉意!
  • 先生。比斯利,我完全按照你说的做了,现在它说“未定义全局名称'文件名'”。
  • wtf 是文件名....?重新阅读我的答案并重新遵循指示...如果您遇到错误,请至少使用错误消息和回溯更新您的原始问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 2012-08-24
  • 2019-12-07
相关资源
最近更新 更多