Unicode issues when requesting form - python

I'm using HTML, Flask and MYSQL to populate a table when the user first loads the page. The table displays all the rows along with a checkbox and when a user presses the submit button, I get the rows that were checked.
Current HTML code
{% for row in data %}
<tr>
<td><input type="checkbox" name="inputSelect" value="{{ row[0], row[3] }}"></td>
<td>{{ row[0] }}</td>
<td>{{ row[1] }}</td>
<td>{{ row[2] }}</td>
<td>{{ row[3] }}</td>
</tr>
{% endfor %}
However, when I try request.form['InputSelect'] on the flask side, it only gives me one result even if I click more than one checkbox.
What's the best way to create the table so that when I click multiple checkboxes, I can see all of them using request.form['InputSelect']

Flask use MultiDict. To get the list of items for a given key, you can use getlist() method.
Try this:
value = request.form.getlist('InputSelect')

Related

passing variable to a table in flask

I am following this video and everything is working perfectly. The output of a SQL query is saved within a variable that I can call on in another .html page.
so my sql query is:
#app.route('/all_data')
def all_data():
customers = db.get_customers()
for customer in customers:
var = customers
return render_template('all_data.html',var=var)
When calling this {{var}} in all_data.html page the output is for a long tuple:
('name','email','comment','gender'),('name','email','comment','gender') etc etc
I am looking to put this {{var}} into a table instead?.. I but am hitting a brick wall in finding out how to do this?
any help would be much appreciated!
Assuming you're using Flask's default template engine (Jinja2), you can simple use a for loop to iterate over the elements of the collection inside the template. The syntax is:
{% for item in items %}
{{ item }}
{% endfor %}
So simply create an HTML table, and add a row for each item in your var variable.
IIRC, Jinja tuples act the same as Python's, so you can probably do something along the lines of:
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Comment</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
{% for current in var %}
<tr>
<td>{{ current[0] }}</td>
<td>{{ current[1] }}</td>
<td>{{ current[2] }}</td>
<td>{{ current[3] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
I haven't checked it (as I don't have a Flask project available right now), but if my memory serves me right, that's how I would do it
Here's a link to an usefull part of Jinja2's documentation
Also, what is this piece of code for ?
for customer in customers:
var = customers
You're iterating over each element of the list, but your never use the customer variable (containing the current element). Instead, you assign the var variable to the customers list multiple times. Assuming customers is a simple Python list, you could remove this loop and simply do return render_template('all_data.html',var=customers)

How to receive a row information from a HTML table to Python Flask

I am developing a simple web front-end with Flask, which displays a table from database, and when an user selects a specific row, the Flask gets the information of a specific column of the selected row.
with the following codes, I display a table with 5 columns of data and one last column of 'submit' button. When the 'submit' button of the specific row, the second column ('Title') information is supposed to be posted back to Flask app.
It works 90%, because when the button is clicked, always the first row information is posted, even if a different row is selected. Could anyone figure what went wrong here?
Thanks!
here is a flask code
#app.route("/", methods=["GET", "POST"])
def home():
df = pd.read_excel('database.xlsx')
this_list = df.values.tolist()
if request.method =="POST":
if request.form.get('select_song') == 'select':
print('selected')
print(request.form.get("title"))
return render_template('basic_table.html', title='Basic Table',
table=this_list)
this is a basic_table.html
<form method="POST">
<table id="data" class="table table-striped">
<thead>
<tr>
<th>Type</th>
<th>Title</th>
<th>Location</th>
<th>Translation</th>
<th>Content</th>
<th>selection</th>
</tr>
</thead>
<tbody>
{% for row in table %}
<tr>
<td>{{ row[0] }}</td>
<td><input type="hidden" name="title" value="{{ row[1] }}"> {{ row[1] }}</td>
<td>{{ row[2] }}</td>
<td>{{ row[3] }}</td>
<td>{{ row[4] }}</td>
<td>
<input class="form__submit form__input" type="submit" value="select" name="select_song"/>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
</form>
When you click a button whose type is 'submit', it will submit the form attached to that submit button. Submitting the form means it will pass html elements by name/value pair.
In your code, you have ONLY 1 form and each row has the same input element with the name title. Therefore when you submit the form, it will submit the first instance of title
There are 2 possible solutions here
You change your code so that you have a form in each row. This way, when you submit, you are submitting the form for just that row.
You keep your code as is, but add Javascript code to 'intercept' your submit action. Your JS code will then determine which row you clicked, pick the value of the title in that row and manually submit the form with that value.

HTML FLASK using {% for %} in JSON data

I am using google firebase which returns data in JSON, compared to SQLAlchemy which returns it in something else.
In SQLAlchemy, I can fetch a database, pass it as a variable in flask render_template, and do something like this to display all the tables.
{% for Order in query %}
<tr>
<td>{{ Order.uuid }}</td>
<td>{{ Order.costusd }}</td>
<td>{{ Order.costcry }}</td>
<td>{{ Order.orderinfo }}</td>
<td>{{ Order.userpaid }}</td>
</tr>
{% endfor %}
If I attempt this with firebase/JSON data, it will display empty tables (the amount of tables that I have data in firebase) but with no data. I tried to do order['uuid']. etc for all of them, but that does not work either.
So, I am trying to display my database in the same way that SQLAlchemy can do it, but i am using a NOSQL database that is JSON.
This was a duplicate
Working code to translate the NoSQL data into jinja2 readable:
r = json.dumps(json_onject)
loaded_r = json.loads(r)
return render_template("report.html", items=loaded_r)

Django - Too slow database record read

I have a database with about 1500 records about one class. I'm also using jQuery Datatables to list the records.
views.py:
records = Animal.objects.all()
return render(request, 'animals.html', {'records': records})
template:
{% for record in records%}
<tr>
<td>{{ record.id }}</td>
<td>{{ record.specie }}</td>
<td>{{ record.category}}</td>
<td>{{ record.quantity }}</td>
</tr>
{% endfor %}
But, it is very very slow. Even reducing the number of records to 100 it takes about 20~45 seconds to fully load a page. Doing the same select to find all 1500 records directly on database takes only 1 second.
As I am using jQuery DataTables to paginate and search the values, is there a way to improve this loading table time?
EDIT:
I used the django debug toolbar and I got these results:

Django: Accessing list attributes in template

I have a SQL query in a Django view and store the results in a variable. As far I know the result should be stored as a list.
def assignments(request):
cursor = connection.cursor()
cursor.execute("SELECT o.article_id, o.amount, m.id, o.create_date, m.status FROM orders o, producers p, machines ma, matches m WHERE ma.producer_id=1 AND m.machine_id = ma.id AND m.order_id = o.id")
articles = cursor.fetchall()
context = {"article_list": articles}
return render(request, 'assignments.html', context)
Then I want to transfer that data row by row in a table in my template.
{% block body %}
<div class="container">
<table class="table">
<thead>...</thead>
<tbody>
{% for articles in article_list %}
<tr>
<td>{{ articles.article_id }}</td>
<td>{{ articles.amount }}</td>
<td>{{ articles.id}}</td>
<td>{{ articles.create_date }}</td>
<td>{{ articles.status }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
Unfortunately the table is empty and is not showing any results.
The query itself should be fine. I tested the query in my database workbench and it is showing the correct results.
How can I access the data stored in variable articles from my template?
PS: I'm far from being a programmer. So I don't really know any programming concepts/language.
Any help is highly appreciated. Thanks!
You have a list of lists. Each element in articles_list is just a list of values from the database; it does not have any knowledge of attributes like "id" or "amount".
As Arpit says in the comments, you should be using Django models for this rather than raw SQL.

Categories

Resources