flask中的current_app
时间: 2024-05-27 20:14:41 浏览: 17
在Flask中,current_app是一个线程安全的全局变量,用于在应用程序上下文中访问应用程序对象。它是一个Flask应用程序的代理,可以用于获取应用程序的配置、注册蓝图、添加路由等操作。current_app可以在任何需要访问应用程序对象的地方使用,例如在视图函数中、命令行脚本中、自定义扩展中等。需要注意的是,在使用current_app之前,必须确保当前线程处于应用程序上下文中,否则会引发异常。可以通过with app.app_context():语句块进入应用程序上下文,确保current_app可以正常使用。
相关问题
app.route('/deleteaccount/<int:account_id>', methods=['POST']) @login_required def delete_account(account_id) : authed = session.get('authed') if not authed : return redirect(url_for('login')) form1 = DeleteAccountForm() account = User.query.get_or_404(account_id) if form1.submit.data and form1.validate() : if current_user.id != account.id : abort(401) flash('Usurpation') logout_user() db.session.delete(account) db.session.commit() session.pop('authed') flash('Disposed') else : abort(400) return redirect(url_for('login')) @db.event.listens_for(User, 'after_delete', named=True) def edit_account(**kwargs) : target = kwargs['target'] for username in [target.username] : if username is not None : dis = Photo.query.filter_by(user_name=username).delete() db.session.begin(subtransactions=True) db.session.commit() ,报错信息:TypeError: scoped_session.begin() got an unexpected keyword argument 'subtransactions',修改报错
在你的代码中,`scoped_session` 不支持 `subtransactions` 参数。你可以使用 `session.begin_nested()` 代替 `scoped_session.begin(subtransactions=True)`,以实现嵌套事务。
以下是修改后的代码:
```
@app.route('/deleteaccount/<int:account_id>', methods=['POST'])
@login_required
def delete_account(account_id):
authed = session.get('authed')
if not authed:
return redirect(url_for('login'))
form1 = DeleteAccountForm()
account = User.query.get_or_404(account_id)
if form1.submit.data and form1.validate():
if current_user.id != account.id:
abort(401)
flash('Usurpation')
logout_user()
db.session.delete(account)
db.session.commit()
session.pop('authed')
flash('Disposed')
else:
abort(400)
return redirect(url_for('login'))
@db.event.listens_for(User, 'after_delete', named=True)
def edit_account(**kwargs):
target = kwargs['target']
for username in [target.username]:
if username is not None:
dis = Photo.query.filter_by(user_name=username).delete()
nested = db.session.begin_nested()
nested.commit()
```
ESP32在arduino中将每分钟的结构体数据在flash中的存取代码
以下是 ESP32 在 Arduino 中将每分钟的结构体数据存储到 Flash 中的示例代码:
```C++
#include <Preferences.h>
struct Data {
float value1;
int value2;
String value3;
};
void saveDataToFlash(Data data) {
Preferences preferences;
preferences.begin("my-app", false);
int minute = minute();
preferences.putBytes(String(minute).c_str(), &data, sizeof(data));
preferences.end();
}
Data getDataFromFlash(int minute) {
Preferences preferences;
preferences.begin("my-app", true);
Data data;
preferences.getBytes(String(minute).c_str(), &data, sizeof(data));
preferences.end();
return data;
}
void setup() {
// 初始化
}
void loop() {
// 获取数据
Data data;
data.value1 = ...;
data.value2 = ...;
data.value3 = ...;
// 获取当前分钟数
int currentMinute = minute();
// 存储数据
saveDataToFlash(data);
// 等待下一分钟
while (minute() == currentMinute) {
delay(1000);
}
}
```
在这个示例中,我们使用了 Preferences 库来存储数据。我们将数据存储在名为 "my-app" 的命名空间中,并使用当前分钟数作为键名。在每个循环周期中,我们获取当前分钟数,并等待下一分钟开始,然后存储数据。您可以根据需要更改结构体的定义和存储位置。请注意,如果要存储大量数据,请考虑使用 SPIFFS 或 LittleFS 文件系统。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)