【问题标题】:jinja2.exceptions.UndefinedError: object has no attributejinja2.exceptions.UndefinedError:对象没有属性
【发布时间】:2020-01-15 05:23:14
【问题描述】:

我正在尝试创建一个库存管理系统,我可以在其中通过表单(WTForms)创建/添加库存并将其显示在单独页面的表格中。目前,我陷入了上述错误。我到处寻找答案无济于事。我应该通过“createGPU.html”添加库存并被定向到“retrieveGPU.html”

点击createGPU中的提交按钮会导致jinja2.exceptions.UndefinedError: 'GPUs.GPUs object' has no attribute '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))

创建GPU

{% 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 %}

检索GPU

{% 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


【问题讨论】:

    标签: python flask shelve


    【解决方案1】:

    Jinja2 处理时

        <td>{{ gpu.GPUName() }}</td>
    

    它期望gpu 将有一个名为GPUName 的可调用成员函数。您编写代码的方式,get_GPUName 是您想要的。

    Python 的方式是完全删除 getter 和 setter,删除双下划线前缀,并让模板直接用于命名的实例变量。

        <td>{{ gpu.GPUName }}</td>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      相关资源
      最近更新 更多