简体   繁体   English

无法使条目显示在flask Web应用程序中

[英]Can't get entries to appear in flask web application

I am trying to create a basic web application that accepts text inputs on the "add queries" page and then displays them in the "search Queries" page. 我正在尝试创建一个基本的Web应用程序,该应用程序在“添加查询”页面上接受文本输入,然后在“搜索查询”页面上显示它们。 So far, I have successfully connected the database, and the website doesn't throw an error when I use it. 到目前为止,我已经成功连接了数据库,并且使用该网站时不会引发任何错误。

However, when I enter an input and click submit on the "Add Queries" page, the "Search Queries" page doesn't update or display any new inputs. 但是,当我输入一个输入并在“添加查询”页面上单击提交时,“搜索查询”页面不会更新或显示任何新输入。

I think that there is something wrong in my code which isn't linking the inputs to the database. 我认为我的代码有问题,没有将输入链接到数据库。

UPDATE: Someone has mentioned that I don't call the methods "add_entry" and "show_entries" in my view subclass. 更新:有人提到我没有在我的视图子类中调用方法“ add_entry”和“ show_entries”。 However, shouldn't my main py file handle functions? 但是,我的主要py文件不应该处理函数吗?

Here are some basic screenshots for the webapp that I am developing: 以下是我正在开发的Web应用程序的一些基本屏幕截图:

在此处输入图片说明

在此处输入图片说明

The following represents my py files: 以下是我的py文件:

This is my main py file (queries-Final2.py) 这是我的主要py文件(queries-Final2.py)

#functions
def show_entries():
    db = get_db()
    cur = db.execute('select title, columns, query, notes, tags from entries order by id desc')
    entries = [dict(title=row[0], columns=row[1],query=row[2],notes=row[3],tags=row[4]) for row in cur.fetchall()]
    return render_template('search-queries.html', entries=entries)

def add_entry():
    if not session.get('logged_in'):
    abort(401)
    db = get_db()
     db.execute('insert into entries (title, columns, query, notes, tags) values (?, ?)',
      [request.form['title'],
       request.form['columns'],
       request.form['query'],
       request.form['notes'],
       request.form['tags']
       ])
    db.commit()
     flash('New entry was successfully posted')
    return redirect(url_for('add-queries'))


# Routes
   app.add_url_rule('/',
   view_func=Main.as_view('main'),
   methods=["GET"])

   app.add_url_rule('/login/',
   view_func=Login.as_view('login'),
   methods=["GET", "POST"])

   app.add_url_rule('/search-queries/',
   view_func=SearchQueries.as_view('search-queries'),
   methods=["GET", "POST"])

   app.add_url_rule('/add-queries/',
   view_func=AddQueries.as_view('add-queries'),
   methods=["GET", "POST"])

   app.add_url_rule('/edit-queries/',
   view_func=EditQueries.as_view('edit-queries'),
   methods=["GET", "POST"])

As well as the class files: 以及类文件:

This is my "Search Queries" page view 这是我的“搜索查询”页面视图

class SearchQueries(flask.views.MethodView):
  @utils.login_required
  def get(self):
    return flask.render_template('search-queries.html')

This is my "Add Queries" page view 这是我的“添加查询”页面视图

class AddQueries(flask.views.MethodView):
  @utils.login_required
  def get(self):
    return flask.render_template('add-queries.html')

  def post(self):
    return flask.render_template('search-queries.html')

These are my html files: 这些是我的html文件:

This is my "add queries" html page 这是我的“添加查询” HTML页面

{% extends "layout.html" %}
{% block content %}
<h1 class="pageHeader" xmlns="http://www.w3.org/1999/html">Add a query</h1>
<h3 class="pageSubHeader">Sooo many queries to add</h3>
<form action="{{ url_for('add-queries') }}" method=post class=add-entry>
    <dl>
        <dt>Title:
        <dd><input type=text size=54 name=title>
        <dt>Columns: (Store ID, Date, transaction-total, etc... )
        <dd><textarea name=columns rows=5 cols=40></textarea>
        <br>
        <dt>Query: (The text of the query)
        <dd><textarea name=query rows=5 cols=40></textarea>
        <br>
        <dt>Notes: (Anything that the analyst should note)
        <dd><textarea name=notes rows=5 cols=40></textarea>
        <br>
        <dt>Tags: (traffic, conversion, etc... )
        <dd><input name=tags type=text size=54>
        <br>
        <dd><input type=submit value="Submit query">
    </dl>
</form>
{% endblock %}

This is my "search queries" html page 这是我的“搜索查询” html页面

{% extends "layout.html" %}
{% block content %}
    <h1 class="pageHeader">Have fun searching for queries</h1>
    <h3 class="pageSubHeader">Search for a large query</h3>
    <ul class=entries>
      {% for entry in entries %}
         <li><h2>{{ entry.title }}</h2>{{ entry.columns|safe }}
      {% else %}
          <li><em>Unbelievable.  No entries here so far</em>
      {% endfor %}
    </ul>
{% endblock %}

There may be something really obvious that I am missing so any help here would be greatly appreciated. 可能确实有一些我很想念的东西,所以在这里的任何帮助将不胜感激。 Thanks in advance! 提前致谢!

  • Lowly intern 低实习生

UPDATE: my database uses SQLite3 更新:我的数据库使用SQLite3

and the schema.sql file is as follows: 并且schema.sql文件如下:

drop table if exists entries;
create table entries (
  id integer primary key autoincrement,
  title string not null,
  columns string not null,
  query string not null,
  notes string not null,
  tags string not null
 );

Question: In the example that I was looking at, the developer used 问题:在我正在查看的示例中,开发人员使用了

 @app.route('/add/')

before declaring the function. 在声明功能之前。 What does this do? 这是做什么的?

You're right, nowhere in your code do you actually call your add_entries and show_entries functions. 没错,您在代码中没有实际调用过 add_entriesshow_entries函数。

I'm not a fan of view classes, so I've never used them, however, would you not do something like this: 我不是视图类的粉丝,所以我从未使用过它们,但是,您不会这样做吗:

class AddQueries(flask.views.MethodView):
  @utils.login_required
  def get(self):
    return flask.render_template('add-queries.html')

  def post(self):
    db = get_db()
    db.execute('insert into entries (title, columns, query, notes, tags) values (?, ?, ?, ?, ?)',
    [request.form['title'],
     request.form['columns'],
     request.form['query'],
     request.form['notes'],
     request.form['tags']
    ])
    db.commit()
    flash('New entry was successfully posted')
    return flask.render_template('search-queries.html')

so basically, when you POST something to the AddQueries view, it actually inserts it into the database. 所以基本上,当你POST东西的AddQueries来看,它实际上将其插入到数据库中。 Similarly, your ShowQueries class would need to actually run the code currently in your show_entries() function. 同样,您的ShowQueries类将需要实际运行show_entries()函数中当前的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM