Passing data from html to python - python

It is necessary to obtain information from the selected cell from the html file and then work with it in views.py. Is it possible and how to do it? Thank you in advance!
<tbody>
{% for element in qz %}
<tr>
<td class="counterCell"></td>
<td style="display: none">{{ element.0 }}</td> <!-- information is needed for this cell when it is selected -->
<td>{{ element.1 }}</td>
<td>{{ element.2 }}</td>
<td><span class="glyphicon glyphicon-trash"></span></td>
</tr>
{% endfor %}
</tbody>
views.py:
cursor = connection.cursor()
cursor1 = connection.cursor()
cursor2 = connection.cursor()
cursor.execute(
"SELECT sites.site_title, sites.url FROM sites, my_sites, auth_user WHERE auth_user.id = my_sites.id_user AND sites.id = my_sites.id_site AND auth_user.id =" + str(
request.user.id))
cursor1.execute(
"SELECT sites.site_title, sites.url FROM sites, my_sites, auth_user WHERE auth_user.id = my_sites.id_user AND sites.id = my_sites.id_site AND auth_user.id =" + str(
request.user.id))
cursor2.execute(
"SELECT sites.id, sites.site_title, sites.url FROM sites, my_sites, auth_user WHERE auth_user.id = my_sites.id_user AND sites.id = my_sites.id_site AND auth_user.id =" + str(
request.user.id))
q = [str(row[0]) for row in cursor.fetchall()]
z = [str(row[1]) for row in cursor1.fetchall()]
x = [str(row[0]) for row in cursor2.fetchall()]
qz = [(x[i], q[i], z[i]) for i in range(len(q))]
return render(request, 'main/my_newsagent.html', {'qz': qz})

That what you showing is a template. Template receive a data from a view function.
So that function located in a views.py returning that template, with interpolated data (rendered final html). So in may be in context or in other variable.

Related

How to implement pagination in a Flask and PostgreSQL application, with sorting based on location?

I am worried I would have to sort the whole table again an again each time a user clicks next page. What is the best way to sort? I am trying to show those posts which are nearer to user's location in ascending order.
#app.route("/page/<int:page>")
def show_page(page):
sort_order = request.args.get("sort_order")
limit = 10
offset = (page - 1) * limit
rows = get_rows(offset, limit, sort_order)
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM table")
total_rows = cursor.fetchone()[0]
cursor.close()
total_pages = (total_rows + limit - 1) // limit
if page > total_pages:
return redirect(url_for("show_page", page=total_pages))
return render_template("page.html", rows=rows, page=page, total_pages=total_pages, sort_order=sort_order)
def get_rows(offset, limit, sort_order):
cursor = conn.cursor()
if sort_order == "asc":
cursor.execute("SELECT * FROM table ORDER BY location ASC LIMIT %s OFFSET %s", (limit, offset))
elif sort_order == "desc":
cursor.execute("SELECT * FROM table ORDER BY location DESC LIMIT %s OFFSET %s", (limit, offset))
else:
cursor.execute("SELECT * FROM table LIMIT %s OFFSET %s", (limit, offset))
rows = cursor.fetchall()
cursor.close()
return rows
<table>
{% if rows %}
{% for row in rows %}
<tr>
<td>{{ row[0] }}</td>
<td>{{ row[1] }}</td>
<td>{{ row[2] }}</td>
...
</tr>
{% endfor %}
{% endif %}
</table>
<div class="pagination">
{% if page > 1 %}
Previous
{% endif %}
{% if page < total_pages %}
Next
{% endif %}
</div>
<div class="sort-order">
Sort by location (Ascending)
Sort by location (Descending)
</div>

Flask FieldList can't change data

I am trying to generate a dynamic list of student from database, using FieldList and FormField. The fields are being generated using the data from database, but if I try to change something from the front-end, the back-end receives only the starting data (e.g., for checkbox it only gets False)
form:
class Beneficiary_Migration_Entry_Form(FlaskForm):
name = StringField('')
id = StringField('')
school = SelectField('', validate_choice=False)
student_class = SelectField('', validate_choice=False)
migrate = BooleanField('')
class Beneficiary_Migration_Form(FlaskForm):
entry_form_list = FieldList(FormField(Beneficiary_Migration_Entry_Form))
submit = SubmitField('Submit')
route
#app.route('/migration/student/<school_year_id>', methods=['GET', 'POST'])
#login_required
def student_migration_page(school_year_id):
school_year_id = int(school_year_id)
school_year = School_Year.query.filter(School_Year.id==school_year_id).first()
entry_form_list = []
school_list = Database_Manager.get_school_list(current_user)
school_choices = get_school_list()
entries = Student.query.filter(Student.user_id==current_user.id).all()
for entry in entries:
form = Beneficiary_Migration_Entry_Form(
name=entry.name,
id=entry.id,
)
id_str = str(entry.id)
form.name.name = 'name-' + id_str
form.school.name = 'school-' + id_str
form.migrate.name = 'migrate-' + id_str
form.student_class.name = 'student_class-' + id_str
form.id.name = 'id-' + id_str
form.school.choices = school_choices
form.student_class.choices = [('','')]
entry_form_list.append(form)
migration_form = Beneficiary_Migration_Form()
migration_form.entry_form_list = entry_form_list
school_map = Database_Manager.get_schools_map(current_user)
if migration_form.submit.data:
for form in migration_form.entry_form_list:
print(form.migrate.data)
return redirect(url_for('migration_page', school_year_id=school_year_id))
return render_template('migration-elev.html', school_map=school_map,school_year=school_year, migration_form=migration_form)
Front-end:
<form action="" method="post">
{{ migration_form.hidden_tag() }}
<table class="table table-striped table-dark" style="text-align: center;">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Nume</th>
<th scope="col">Scoala</th>
<th scope="col">Clasa/ Grupa</th>
<th scope="col">Transfer</th>
</tr>
</thead>
<tbody>
{% for form in migration_form.entry_form_list %}
{{ form.hidden_tag() }}
<tr>
<td>{{form.id.value}}</td>
<td>{{form.name}}</td>
<td>{{form.school}}</td>
<td>{{form.student_class}}</td>
<td>{{form.migrate}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<p>{{ migration_form.submit(class_="btn btn-primary") }}</p>
</form>
At first it only got the data from the first entry from the FieldList, but I've found that I need to put unique names to work.
I've tried to use validate_on_submit but I get AttributeError: 'Beneficiary_Migration_Form' object has no attribute 'copy'.
I've also tried to use dictionaries and named tuples to load data, but I get the same result.
I've modified name to entry_name, since name is a reserved word.
To load data I've used append_data, where I've made a dictionary with data from db.

How to limit digits after decimal point in SQlite python?

Hi, sorry if it seems such simple but honestly the solution I find on google (or my implementation) is breaking my code :/
How can i limit the digits as 2 after decimal point. Even it's 2.99999999 I would like to display as 2.30.
TIA
Here my code for this page (working but displays lots of digits after decimal point):
#app.route("/")
#login_required
def index():
"""Show portfolio of stocks"""
# Update the latest price information for the existing stock
user_id = session["user_id"]
stock_list = db.execute("SELECT stock_id, stock_symbol, shares, unit_price, total_price FROM stocks WHERE owner_name == %s", session["user_id"])
# Iterate through dictionaries in the results (list of rows(dicts))
length = len(stock_list)
for i in range(length):
stock_id = stock_list[i]["stock_id"]
symbol = stock_list[i]["stock_symbol"]
amount = stock_list[i]["shares"]
price_dict = lookup(symbol)
price = price_dict["price"]
total = price * amount
# Update stocks table for the logged in user
db.execute("UPDATE stocks SET unit_price = ?, total_price = ? WHERE stock_id = ?", price, total, stock_id)
# Extract updated data and display in the template
rows = db.execute("SELECT stock_symbol, stock_name, shares, unit_price, total_price FROM stocks WHERE owner_name == %s", session["user_id"])
rows2 = db.execute("SELECT cash FROM users WHERE username == %s", session["user_id"])
assets_list = db.execute("SELECT total_price FROM stocks WHERE owner_name == %s", session["user_id"])
stock_assets = 0.00
cash_asset_list = db.execute("SELECT cash FROM users WHERE username == %s", session["user_id"])
cash_asset = cash_asset_list[0]["cash"]
for i in range(len(assets_list)):
stock_assets = stock_assets + assets_list[i]["total_price"]
net_assets = stock_assets + cash_asset
return render_template("index.html", rows=rows, rows2=rows2, net_assets=net_assets)
My HTML template for the page
{% extends "layout.html" %}
{% block title %}
Home
{% endblock %}
{% block main %}
<table class="table table-hover">
<tr class="table-info">
<th scope="col">Symbol</th>
<th scope="col">Company</th>
<th scope="col">Shares</th>
<th scope="col">Price</th>
<th scope="col">TOTAL</th>
</tr>
{% for row in rows %}
<tr class="table-light">
<td>{{ row.stock_symbol }}</td>
<td>{{ row.stock_name }}</td>
<td>{{ row.shares }}</td>
<td>{{ row.unit_price }}</td>
<td>{{ row.total_price }}</td>
</tr>
{% endfor %}
</table>
<table class="table table-hover">
<tr class="table-dark">
<th scope="col">Cash Balance</th>
<th scope="col">Total Assets</th>
</tr>
{% for row2 in rows2 %}
<tr class="table-light">
<td>{{ row2.cash }}</td>
<td>{{ net_assets }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
You can create a function:
def truncate(f):
return math.floor(f * 10 ** 2) / 10 ** 2
And it will return the value without rounding with 2 decimal places.
Then when you do total just set it to the function of the result.
In python you can use the function round to limit the decimal for number of places.
x = 1.4499999
you can use round(x, number of decimal places)
x = round(x, 2)
Final output will be
x = 1.50

Update SQL query for HTML table Python

I have the following code in my Python project:
<table class="table table-striped">
<tr>
<th>Card Number</th>
<th>Card Name</th>
<th>Card Rarity</th>
<th>Card Quantity</th>
</tr>
{% for card in card_data %}
<tr>
<td>{{card.card_prefix}}</td>
<td>{{card.card_name}}</td>
<td>{{card.card_rarity}}</td>
<td>{{card.card_quantity}}</td>
</tr>
{% endfor %}
</table>
Is there a way to update the quantity to -1 or +1 on a button that only updates one row of the SQL database?
The current code in my main page is:
#app.route('/lob')
def lob():
session.clear()
cur = mysql.connection.cursor()
current_set = 'LOB'
session['set_selected'] = True
session['current_set'] = current_set
result = cur.execute("SELECT * FROM card_list WHERE card_set =%s", [current_set])
card_data = cur.fetchall()
if result > 0:
return render_template('/Booster Packs/lob.html', card_data=card_data)
else:
flash('Set Not Found!', 'danger')
return render_template('/Booster Packs/lob.html')
I can update the question with more information if needed, I'm just not sure how to go about how to select the value to update given the table.
Sorry for the bad English, it is not my first language.

Flask BuildError - Could not build URL for endpoint

I've got what should be a very simple python code to select some data from my database
#app.route('/studentsearch', methods = ['POST', 'GET'])
def searchstudent():
""" Displays the student search page for the site """
cursor = mydb.cursor()
cursor.execute ("SELECT * FROM student")
all_students = cursor.fetchall()
cursor.close()
return render_template('studentsearch.html', all_students = all_students)
But when I go to render the HTML page i get werkzeug.routing.BuildError: Could not build url for endpoint 'studentsearch'. Did you mean 'newstudent' instead?
My table is also pretty straight forward as well...
<table>
<thread>
<tr>
<th>Student ID:</th>
<th>First Name:</th>
<th>Last Name:</th>
<th>Click to View Student Details</th>
</tr>
</thread>
<tbody>
{% for each_result in all_students %}
<tr>
<td>{{ each_result[0] }}</td>
<td>{{ each_result[1] }}</td>
<td>{{ each_result[2] }}</td>
<td>CLICK HERE!!!</td>
</tr>
{% endfor %}
</tbody>
</table>
What's going on? 'newstudent' isn't even mentioned within the python or HTML??
As requested here's the code that the 'newstudent' appears to be coming from:
def newstudent():
"""Displays the new student page for the site"""
return render_template('newstudent.html')
#app.route('/process_newstudent', methods = ['POST', 'GET'])
def process_newstudent():
""" Processes the data from the new student course """
if request.method == 'POST':
first_name = request.form['firstname']
last_name = request.form['lastname']
year_began = request.form['yearbegan']
try:
cursor = mydb.cursor()
cursor.execute ("INSERT INTO student (first_name, last_name, year_began) VALUES ('{}','{}','{}');".format((first_name.title()), (last_name.title()), year_began))
mydb.commit()
cursor.close()
except mysql.connector.Error as err:
cursor.close()
return failure('newstudent', f"Error message: {err}. Student not added")
else:
cursor = mydb.cursor()
cursor.execute ("SELECT * FROM student")
student_success = cursor.fetchall()
cursor.close()
return render_template('newstudent_success.html', student_success = student_success)
else:
request.method == 'GET'
return render_template('newstudent.html')
Are you trying to build a dynamic URL somewhere in your application code or HTML code using the url_for function?
If you are, then I think what might be happening is that you're passing the parameter 'studentsearch' to that function instead of 'searchstudent' (url_for builds endpoints using the view function name, not the endpoint name). E.g. within an HTML template the URL in question would be built like this:
Search

Categories

Resources