Reputation: 346
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. 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. However, shouldn't my main py file handle functions?
Here are some basic screenshots for the webapp that I am developing:
The following represents my py files:
This is my main py file (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:
This is my "add queries" html page
{% 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
{% 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!
UPDATE: my database uses SQLite3
and the schema.sql file is as follows:
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?
Upvotes: 0
Views: 1666
Reputation: 3295
You're right, nowhere in your code do you actually call your add_entries
and show_entries
functions.
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. Similarly, your ShowQueries
class would need to actually run the code currently in your show_entries()
function.
Upvotes: 1