jinja2.exceptions.UndefinedError: у объекта нет атрибута - PullRequest
0 голосов
/ 15 января 2020

Я пытаюсь создать систему управления запасами, в которой я могу создавать / добавлять запасы через форму (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


1 Ответ

1 голос
/ 15 января 2020

Когда Jinja2 обрабатывает

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

, он ожидает, что gpu будет иметь вызываемую функцию-член с именем GPUName. То, как вы кодировали вещи, get_GPUName - это то, что вы хотите вместо этого.

Путь Pythoni c заключался в том, чтобы полностью отбросить геттеры и сеттеры, удалить префиксы с двойным подчеркиванием и получить шаблон go непосредственно для именованной переменной экземпляра.

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