NameError:未定义名称“标记”

时间:2020-01-31 08:35:40

标签: python flask cassandra cql

在这里,我尝试使用filter()从cassandra中获取数据,我需要在其中获取大于或等于65分的学生,但我收到此错误,无法理解为什么收到此错误。我指的是this链接。我也提到了类似的问题,但是没有任何解决方案。 这是我的python代码:

from flask import *
from flask_cqlalchemy import CQLAlchemy

app = Flask(__name__)
app.config['CASSANDRA_HOSTS'] = ['127.0.0.1']
app.config['CASSANDRA_KEYSPACE'] = "emp"

db = CQLAlchemy(app)

class Student(db.Model):
    uid = db.columns.Integer(primary_key=True)
    marks = db.columns.Integer(primary_key=True)
    username = db.columns.Text(required=True)
    password = db.columns.Text()

@app.route('/merit')
    def show_merit_list():
        ob = Student.objects.filter(marks >= 65) 
        return render_template('merit.html', ml = ob)

这是我得到的错误日志:

Traceback (most recent call last)
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 2463, in 
__call__
return self.wsgi_app(environ, start_response)
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 2449, in 
wsgi_app
response = self.handle_exception(e)
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 1866, in 
handle_exception
reraise(exc_type, exc_value, tb)
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/_compat.py", line 39, in 
reraise
raise value
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 2446, in 
wsgi_app
response = self.full_dispatch_request()
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 1951, in 
full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 1820, in 
handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/_compat.py", line 39, in 
reraise
raise value
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 1949, in 
full_dispatch_request
rv = self.dispatch_request()
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 1935, in 
dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/sudarshan/Downloads/PycharmProjects/try/try1.py", line 67, in show_merit_list
ob = Student.objects.filter(marks >= 65)
NameError: name 'marks' is not defined

3 个答案:

答案 0 :(得分:2)

self对象传递给您的方法,从而允许其访问marks数据成员。

marks更改为self.marks

    @app.route('/merit')
    def show_merit_list(self):
        ob = Student.objects.filter(self.marks >= 65) 
        return render_template('merit.html', ml = ob)

答案 1 :(得分:1)

最后,我找到了忘记使用allow_filtering()的答案。该代码将如下所示:

var relativeTo = path.dirname(grunt.file.expand(f)[0]);

答案 2 :(得分:0)

您需要使用Filtering Operators,请尝试:

ob = Student.objects.filter(marks__gte=65)