pythonbegineer pythonbegineer - 1 month ago 5
Python Question

Changing values using POST method in FLASK?

I'm new in StackOverflow and Python. I'm trying to make a basic application where I have my < form > tag, my flask app, and my .txt. I'm trying to change values but it's not working or I don't know why it's not working. Could any of you give me a hand?

Python Flask:

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>




My .txt file:


I want to change this.


It's returning the value of "this".

Basically when I run my app it's displaying "this" when I try to edit for "Hello" it's returning me an error:



  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) Line
    Found! : this.
    127.0.0.1 - - [21/Oct/2016 13:09:07] "GET / HTTP/1.1" 200 - global name 'this' is not defined
    127.0.0.1 - - [21/Oct/2016 13:09:30] "POST / HTTP/1.1" 200 -




My output:
Please click here to see my output

I'm sorry if this is a silly question, or if this was answered before, but I've been browsing for an answer for time and I have found nothing about this, I've tried different codes but not working, been watching youtube videos and everything.

If someone know the solution would be really helpful for me.

It's for learning and Python School. Thanks!

Working on Python 2.7

EDIT

I updated code with the suggestions below but it's not working yet.
it's not overwriting my "this" with replace.

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)

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

should be

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

(you will also need import fileinput)

you need to change this because this.input doesnt mean anyhthing to python ... it has no idea what this is ...

global name 'this' is not defined

as an aside you should really run with app.run(debug=True) while developing

Comments