我正在尝试创建一个库存管理系统,在该系统中,我可以通过表单(WTForms)创建/添加库存并将其显示在单独页面的表格中。目前,我坚持所说的错误。我到处都在寻找无济于事的答案。我应该通过'createGPU.html'添加库存,并定向到'retrieveGPU.html'
单击createGPU中的提交按钮将导致jinja2.exceptions.UndefinedError:'GPUs.GPUs对象'没有属性'GPUName'
主程序
from flask import Flask, render_template, request, redirect, url_for
from Forms import CreateGPUForm
import shelve, GPUs
@app.route("/createGPU", methods=['GET', 'POST'])
def createGPU():
createGPUForm = CreateGPUForm(request.form)
if request.method == 'POST' and createGPUForm.validate():
gpuDict = {}
db = shelve.open('storage.db', 'c')
try:
gpuDict = db['GPU']
except:
print('Error in retrieving GPUs from storage.db.')
gpu = GPUs.GPUs(createGPUForm.GPUName.data, createGPUForm.GPUUnitPrice.data, createGPUForm.GPUStock.data,createGPUForm.GPUSales.data)
gpuDict[gpu.get_GPUNo()] = gpu
db['GPU'] = gpuDict
db.close()
return redirect(url_for('retrieveGPU'))
return render_template('createGPU.html', form=createGPUForm)
if __name__ == '__main__':
app.run()
@app.route('/retrieveGPU')
def retrieveGPU():
gpuDict = {}
db = shelve.open('storage.db', 'r')
gpuDict = db['GPU']
db.close()
gpuList = []
for key in gpuDict:
gpu = gpuDict.get(key)
gpuList.append(gpu)
return render_template('retrieveGPU.html',gpuList=gpuList, count=len(gpuList))
createGPU
{% extends "base.html" %}
{% block title %}GPU-Assist - Add Graphics Card{% endblock %}
{% block content %}
{% from "includes/_formhelper.html" import render_field %}
<h1 class="display-4">Add Graphics Card</h1>
<form method="POST" action="">
<div class="form-group">
{{ render_field(form.GPUName, class="form-control") }}
</div>
<div class="form-group">
{{ render_field(form.GPUStock, class="form-control") }}
</div>
<div class="form-group">
{{ render_field(form.GPUUnitPrice, class="form-control") }}
</div>
<div class="form-group">
{{ render_field(form.GPUSales, class="form-control") }}
</div>
<input type="submit" value="Submit" class="btn btn-primary"/>
</form>
{% endblock %}
retrieveGPU
{% extends "base.html" %}
{% block title %}GPU-Assist - View GPU-Cart{% endblock %}
{% block content %}
<h1 class="display-4">View GPU</h1>
<div>
{% if count == 0 %}
<p>No GPU in recorded in inventory.</p>
{% elif count == 1 %}
<p>There is 1 GPU recorded in the inventory.</p>
{% else %}
<p>There are {{ count }} GPUs recorded in the inventory.</p>
{% endif %}
</div> <!--End of display number of users div-->
<div>
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Stock</th>
<th>Unit Price</th>
<th>Sales</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{% for gpu in gpuList %}
<tr>
<td>{{ gpu.GPUName() }}</td>
<td>{{ gpu.GPUStock() }}</td>
<td>{{ gpu.GPUUnitPrice() }}</td>
<td>{{ gpu.GPUSales() }}</td>
<td><a href="/updateGPU/{{gpu.get_GPUNo()}}" class="btn btn-warning">Update</a></td>
<td>
<form action="" method="POST">
<input type="submit" value="Delete" class="btn btn-danger">
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
GPUs.py
class GPUs:
countID = 0
def __init__(self,GPUName,GPUStock,GPUUnitPrice,GPUSales):
GPUs.countID += 1
self.__GPUName = GPUName
self.__GPUStock = GPUStock
self.__GPUUnitPrice = GPUUnitPrice
self.__GPUSales = GPUSales
self.__GPUNo=GPUs.countID
'''
One obvious limitation it has is that every time you restart the web application, the countID resets to 0.
Once the countID resets, the next newly created Users’s userID will start from 1 again and overwrite any User
that previously had __userID = 1. (Must figure out an alternative)
'''
#Getter or Accessor
def get_GPUNo(self):
return self.__GPUNo
def get_GPUName(self):
return self.__GPUName
def get_GPUStock(self):
return self.__GPUStock
def get_GPUUnitPrice(self):
return self.__GPUUnitPrice
def get_GPUSales(self):
return self.__GPUSales
#Settor or Mutator
def set_GPUNo(self, GPUNo):
self.__GPUNo = GPUNo
def set_GPUName(self, GPUName):
self.__GPUName = GPUName
def set_GPUStock(self, GPUStock):
self.__GPUStock = GPUStock
def set_GPUUnitPrice(self, GPUUnitPrice):
self.__GPUUnitPrice = GPUUnitPrice
def set_GPUSales(self, GPUSales):
self.__GPUSales = GPUSales
Jinja2处理时
<td>{{ gpu.GPUName() }}</td>
它期望gpu
将具有一个名为GPUName
的可调用成员函数。您编码的方式是get_GPUName
,而不是您想要的。
Python的方法是完全删除getter和setter,删除双下划线前缀,并使模板直接用于命名实例变量。
<td>{{ gpu.GPUName }}</td>