Flask-继承-python自身问题
我有一个小flask应用程序,它主要是用一个类的方法编写的 有人能给我解释一下——举个例子就好了——为什么在将self添加到方法声明之后我会收到1个缺少的参数 下面是一个代码:Flask-继承-python自身问题,python,oop,flask,self,Python,Oop,Flask,Self,我有一个小flask应用程序,它主要是用一个类的方法编写的 有人能给我解释一下——举个例子就好了——为什么在将self添加到方法声明之后我会收到1个缺少的参数 下面是一个代码: from __future__ import print_function from flask import Flask, render_template, make_response from flask import redirect, request, jsonify, url_for from multipro
from __future__ import print_function
from flask import Flask, render_template, make_response
from flask import redirect, request, jsonify, url_for
from multiprocessing import Value
from flask_classful import FlaskView
#from settings import *
from utils.settings import *
import logging
import linecache
import os
import simplejson as json
import mysql.connector
counter = Value('i', 0)
app = Flask(__name__)
class FlaskCLS(FlaskView):
app.debug = False
app._static_folder = os.path.abspath("templates/static/")
@app.route('/v1/api/cords', methods=['GET', 'POST', 'OPTIONS'])
def snd_post():
res = make_response({'x': '55.333', 'y': '18.666'})
return res
@app.route('/', methods=['GET', 'POST', 'OPTIONS'])
def index():
result = []
data = request.data
with counter.get_lock():
counter.value += 1
out = counter.value
with open(str(out), 'a') as dumping:
json.dump(data, dumping)
# ----------------------parser-section------------
with open(str(out), 'r+') as file:
content = file.readlines()
for line in content:
result.append(line.replace('\\', "").replace('"', "").
replace('{', "").replace('}', '').
replace(']', '').replace(',','\n').
replace('[',''))
f = open(str(out), 'w')
f.write('')
f.close()
with open(str(out), 'a') as appending:
appending.write('')
for line in result:
appending.write(line)
# --------------------cords-grabbing-------------
x = linecache.getline(str(out), 1)
y = linecache.getline(str(out), 2)
fx = open('x', 'w')
fy = open('y', 'w')
fx.write(x)
fx.close()
fy.write(y)
fy.close()
mydb = mysql.connector.connect(
host=db_host,
user='root',
auth_plugin='mysql_native_password',
password=password,
buffered=buffered,
database=database)
mycursor = mydb.cursor()
mycursor.execute('select nazwa, x, y, SQRT(POW(69.1 * (x - 54.111), 2) + POW(69.1 *(18.222 - y)\
* COS(x / 57.3), 2)) as distance from wet_dane having distance <125 order by distance;')
mycursor.fetchall()
mydb.commit()
# ---------------------empty-files----------------
str_directory = '/var/www/html/'
list_files = [x for x in os.listdir(str_directory) if x[0] != '.']
for each_file in list_files:
file_path = '%s/%s' % (str_directory, each_file)
if os.path.getsize(file_path) == 0:
os.remove(file_path)
else:
pass
return render_template('layouts/index.html')
flask = FlaskCLS()
# ---------------------runner-section-------------
if __name__ == '__main__':
log = logging.getLogger('werkzeug')
log.disabled = True
app.run(host, port, debug, ssl_context=context)
我的观点是-如何正确地将self添加到index和snd_post方法中?
我想在同一个类中将x变量从index传递到snd_post。
没有自我保护,我不允许做这样的手术
谢谢。我没有使用Flask Classful的经验,但看起来您应该通过以下方式注册应用程序的路由: 类QuotesViewFlaskView: def indexself: 打招呼 QuotesView.registerapp
而不是@app.route。。。decorator.好的,我已经为此制定了一个解决方案,现在我的主python文件如下所示:
from __future__ import print_function
from flask import Flask, render_template, make_response
from multiprocessing import Value
from utils.settings import *
from py_methods.post import Post
from py_methods.db_connect import Db_conn
from py_methods.empty_files_del import DeleteEmpty
from py_methods.files_operations import Files
import logging
import os
app = Flask(__name__)
app.debug = False
app._static_folder = os.path.abspath("templates/static/")
@app.route('/v1/api/cords', methods=['GET', 'POST', 'OPTIONS'])
def snd_post():
return Post.snd_post()
class Main():
@app.route('/', methods=['GET', 'POST', 'OPTIONS'])
def index():
Files.parsing()
DeleteEmpty.del_empty()
Db_conn.wet_db()
return render_template('layouts/index.html')
# ---------------------runner-section-------------
if __name__ == '__main__':
log = logging.getLogger('werkzeug')
log.disabled = True
app.run(host, port, debug, ssl_context=context)
所以它只是一个运行程序——我编写的每个方法都有自己的文件和类。这允许我继承变量。我们可以结束这个话题。我以前从未见过Flask的基于类的视图。该项目自2014年以来一直未更新。除非你有非常特殊的原因,否则可能不会使用它。请始终添加导入statements@J.G.进口增加。对于类,我使用flask classful。也许你有办法解决我的问题?是的。不要使用它。它没有维护。只需使用基于函数的视图