首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >py2neo和flask查询

py2neo和flask查询
EN

Stack Overflow用户
提问于 2016-08-20 15:34:47
回答 1查看 370关注 0票数 0

我在我的应用程序中有问题,我正在尝试用py2neo在flask中运行代码。我有最新版本的NEO4j和python2.7

以下是我在USER类中使用函数的代码

代码语言:javascript
复制
class User:
    def __init__(self, username):
            self.username = username
    def find(self):
            user = graph.find_one("User", "username", self.username)
    def add_challenge(self,challenge_title,total_question_per_user,challengecat,percentage_question,prize,ranks,challenge_status):
            query = '''
            MATCH (u:User),(p:Prize),(ca:Category)
            WHERE u.username = {username} and p.pid = {prize} and ca.catname = {challengecat}
            CREATE (ch:Challenge {chid: str(uuid.uuid4()),challenge_title: {challenge_title}, total_question_per_user: {total_question_per_user},challenge_status: {challenge_status},timestamp:timestamp(),date:date()}),
            (p)-[:BELONG {rank: {ranks} }]->(ch),(ca)-[:BELONG {percentage_question: {percentage_question} }]->(ch)
            '''

            return graph.run(query,username=self.username,prize=prize,challengecat=challengecat,challenge_title=challenge_title,total_question_per_user=total_question_per_user,challenge_status=challenge_status,ranks=ranks,percentage_question=percentage_question)

我是从我的视图文件调用的,并且我在视图文件中导入了用户类,但是当我运行这个页面时,它显示错误

这是代码f视图文件

代码语言:javascript
复制
@app.route('/admin/add/challenge', methods = ['GET', 'POST'])
def admin_add_challenge():
    if not session.get('username'):
            return redirect(url_for('admin_login'))
    if request.method == 'POST':
            challenge_title = request.form['challenge_title']
            total_question_per_user = request.form['total_question_per_user']
            challengecat = request.form['challengecat']
            percentage_question = request.form['percentage_question']
            prize = request.form['prize']
            ranks = request.form['ranks']
            challenge_status = request.form['challenge_status']

            if not challenge_title or not total_question_per_user or not ranks:
                    if not challenge_title:
                            flash('Please Enter Challenge')
                    if not total_question_per_user:
                            flash('Please Enter Number of question Per Player')
                    if not ranks:
                            flash('Please Enter Ranks for win this Challenge')
            else:
User(session['username']).add_challenge(challenge_title,total_question_per_user,challengecat,percentage_question,prize,ranks,challenge_status)
                    flash('Challenge Added successfully')
                    return redirect(url_for('admin_add_challenge'))

    categories = get_categories()
    prizes = get_prizes()
    return render_template('admin/admin_add_challenge.html',categories=categories,prizes=prizes)

在页面http://sitename/admin/add/challenge上提交质询表单时出现以下错误

代码语言:javascript
复制
ERROR in app: Exception on /admin/add/challenge [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/root/gamepro/ddqcore/views.py", line 430, in admin_add_challenge
    User(session['username']).add_challenge(challenge_title,total_question_per_user,challengecat,percentage_question,prize,ranks,challenge_status)
  File "/root/gamepro/ddqcore/models.py", line 285, in add_challenge
    return graph.run(query,username=self.username,prize=prize,challengecat=challengecat,challenge_title=challenge_title,total_question_per_user=total_question_per_user,challenge_status=challenge_status,ranks=ranks,percentage_question=percentage_question)
  File "/usr/local/lib/python2.7/site-packages/py2neo/database/__init__.py", line 731, in run
    return self.begin(autocommit=True).run(statement, parameters, **kwparameters)
  File "/usr/local/lib/python2.7/site-packages/py2neo/database/__init__.py", line 1277, in run
    self.finish()
  File "/usr/local/lib/python2.7/site-packages/py2neo/database/__init__.py", line 1296, in finish
    self._sync()
  File "/usr/local/lib/python2.7/site-packages/py2neo/database/__init__.py", line 1286, in _sync
    connection.fetch()
  File "/usr/local/lib/python2.7/site-packages/py2neo/packages/neo4j/v1/bolt.py", line 337, in fetch
    self.acknowledge_failure()
  File "/usr/local/lib/python2.7/site-packages/py2neo/packages/neo4j/v1/bolt.py", line 284, in acknowledge_failure
    fetch()
  File "/usr/local/lib/python2.7/site-packages/py2neo/packages/neo4j/v1/bolt.py", line 337, in fetch
    self.acknowledge_failure()
  File "/usr/local/lib/python2.7/site-packages/py2neo/packages/neo4j/v1/bolt.py", line 284, in acknowledge_failure
    fetch()
  File "/usr/local/lib/python2.7/site-packages/py2neo/packages/neo4j/v1/bolt.py", line 322, in fetch
    raw.writelines(self.channel.chunk_reader())
  File "/usr/local/lib/python2.7/site-packages/py2neo/packages/neo4j/v1/bolt.py", line 173, in chunk_reader
    chunk_header = self._recv(2)
  File "/usr/local/lib/python2.7/site-packages/py2neo/packages/neo4j/v1/bolt.py", line 156, in _recv
    raise ProtocolError("Server closed connection")
ProtocolError: Server closed connection
49.32.44.55 - - [20/Aug/2016 06:49:05] "POST /admin/add/challenge HTTP/1.1" 500 -
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-23 14:27:56

在Python2.7和py2neo版本3中,我们不能像这样使用查询,我们需要像这样查询

代码语言:javascript
复制
selector = NodeSelector(graph)
            selected_user = selector.select("User", username=user)
            selected_prize = selector.select("Prize", pid=prize)
            selected_cat = selector.select("Category",catname = challengecat)
            challenge = Node("Challenge",chid=str(uuid.uuid4()),challenge_title=challenge_title,total_question_per_user=total_question_per_user,challenge_status=challenge_status,timestamp=timestamp(),date=date())
            rel = Relationship(selected_user,"ADDED",challenge)
            rel1 = Relationship(selected_prize,"BELONG",challenge)
            rel2 = Relationship(selected_cat,"BELONG",challenge)
            graph.create(rel)
            graph.create(rel1)
            graph.create(rel2)

感谢CSR

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39051685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档