SqlAlchemy将模型传递给html代码使我的“ str对象”没有属性错误

时间:2018-12-28 05:43:54

标签: python html flask sqlalchemy

我的sqlalchemy数据库模型返回为

return f"User('{self.user_a}','{self.email}','{self.level}')

※当我检查网络请求标头时

b: User('aaa','kep@keo.com','ccc')

在我的python代码中

a.py selected = request.args.get('b') return render_template('abc.html', selected = selected)

在我的html代码中

abc.html <input type="text" value={{ selected.user_a.data }}> // I guess here is the problem.

我希望输入值显示在主页窗口中。

但是这里出现错误

jinja2.exceptions.UndefinedError : 'str object' has no attribute 'user'

有人可以帮助我解决此错误吗?

1 个答案:

答案 0 :(得分:0)

此异常意味着您正在尝试从字符串而不是对象访问属性。您期望 public async Task InitializeAsync() { //TODO: Use a semaphore lock here if (_IsInitializing) { return; } _IsInitializing = true; try { //TODO: //Dispose(); var isPermissionGranted = await RequestPermissionAsync(); if (!isPermissionGranted.HasValue) { throw new Exception("User did not respond to permission request"); } if (!isPermissionGranted.Value) { throw new Exception("The user did not give the permission to access the device"); } var usbInterface = _UsbDevice.GetInterface(0); //TODO: This selection stuff needs to be moved up higher. The constructor should take these arguments for (var i = 0; i < usbInterface.EndpointCount; i++) { var ep = usbInterface.GetEndpoint(i); if (_ReadEndpoint == null && ep.Type == UsbAddressing.XferInterrupt && ep.Address == (UsbAddressing)129) { _ReadEndpoint = ep; continue; } if (_WriteEndpoint == null && ep.Type == UsbAddressing.XferInterrupt && (ep.Address == (UsbAddressing)1 || ep.Address == (UsbAddressing)2)) { _WriteEndpoint = ep; } } //TODO: This is a bit of a guess. It only kicks in if the previous code fails. This needs to be reworked for different devices if (_ReadEndpoint == null) { _ReadEndpoint = usbInterface.GetEndpoint(0); } if (_WriteEndpoint == null) { _WriteEndpoint = usbInterface.GetEndpoint(1); } if (_ReadEndpoint.MaxPacketSize != ReadBufferLength) { throw new Exception("Wrong packet size for read endpoint"); } if (_WriteEndpoint.MaxPacketSize != ReadBufferLength) { throw new Exception("Wrong packet size for write endpoint"); } _UsbDeviceConnection = UsbManager.OpenDevice(_UsbDevice); if (_UsbDeviceConnection == null) { throw new Exception("could not open connection"); } if (!_UsbDeviceConnection.ClaimInterface(usbInterface, true)) { throw new Exception("could not claim interface"); } Logger.Log("Hid device initialized. About to tell everyone.", null, LogSection); IsInitialized = true; RaiseConnected(); return; } catch (Exception ex) { Logger.Log("Error initializing Hid Device", ex, LogSection); } _IsInitializing = false; } public override async Task<byte[]> ReadAsync() { try { var byteBuffer = ByteBuffer.Allocate(ReadBufferLength); var request = new UsbRequest(); request.Initialize(_UsbDeviceConnection, _ReadEndpoint); request.Queue(byteBuffer, ReadBufferLength); await _UsbDeviceConnection.RequestWaitAsync(); var buffers = new byte[ReadBufferLength]; byteBuffer.Rewind(); for (var i = 0; i < ReadBufferLength; i++) { buffers[i] = (byte)byteBuffer.Get(); } //Marshal.Copy(byteBuffer.GetDirectBufferAddress(), buffers, 0, ReadBufferLength); Tracer?.Trace(false, buffers); return buffers; } catch (Exception ex) { Logger.Log(Helpers.ReadErrorMessage, ex, LogSection); throw new IOException(Helpers.ReadErrorMessage, ex); } } public override async Task WriteAsync(byte[] data) { try { var request = new UsbRequest(); request.Initialize(_UsbDeviceConnection, _WriteEndpoint); var byteBuffer = ByteBuffer.Wrap(data); Tracer?.Trace(true, data); request.Queue(byteBuffer, data.Length); await _UsbDeviceConnection.RequestWaitAsync(); } catch (Exception ex) { Logger.Log(Helpers.WriteErrorMessage, ex, LogSection); throw new IOException(Helpers.WriteErrorMessage, ex); } } 作为对象,但实际上,它是一个字符串。您需要先将其转换为JSON,然后再将其传递到模板中。

selected