I am trying to get data from a table from an HTML that I created, that table has data from a table from a sqlite database.
So, for each checkbox marked in that table, I need to get the data from that row.
However, I am only receiving data from the first line, even if I mark several lines.
I've tried using the getlist, and creating an array, but without success. Since I am still learning from Flask and etc., I already looked for examples but I also couldn't find a solution.
My HTML table:
HTML code:
<form method="POST">
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Id_Game</th>
<th style="width: 800px;">Game</th>
<th style="width: 100x;">Critic Score</th>
<th style="width: 100px;"><input type="checkbox" id="all" onclick="checkAll(this)"></th>
</tr>
</thead>
<tbody>
{% for game in games.items %}
<tr>
<td name="ID_GAME" id="ID_GAME">{{ game.ID_GAME}}</td>
<input type="hidden" name="ID_GAME" value="{{ game.ID_GAME }}" />
<td name="NM_GAME"> {{ game.NM_GAME}} </td>
<input type="hidden" name="NM_GAME" value="{{ game.NM_GAME }}" />
<td name="NR_CRITICSCORE">{{ game.NR_CRITICSCORE}}</td>
<td>
<input type="checkbox" id="one_checkbox" name="one_checkbox">
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% if current_user.is_authenticated %}
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
{% else %}
<div class="form-group">
<button type="submit" class="btn btn-primary" onclick="submitoff()">Submit</button>
</div>
{% endif %}
</form>
Python code:
#main.route('/games/<int:page_num>', methods=('GET', 'POST'))
def games(page_num):
games = V_GAMES.query.paginate(per_page=10, page=page_num, error_out=True)
if request.method =='POST':
if request.form.getlist('one_checkbox'):
ID_USER = current_user.id
ID_GAME = request.form.get('ID_GAME')
NM_GAME = request.form.get('NM_GAME')
IC_PLAYED = "YES"
SYSDATE = datetime.datetime.now()
# create new user with the form data. Hash the password so plaintext version isn't saved.
addprofile = USERGAMESPLAYED(ID_USER=ID_USER, ID_GAME=ID_GAME, NM_GAME=NM_GAME, IC_PLAYED=IC_PLAYED, DT_PLAYED=SYSDATE)
# add the new user to the database
db.session.add(addprofile)
db.session.commit()
flash('Games have been successfully added to your profile.')
if not request.form.get('one_checkbox'):
flash('You have to check at least one game to add to your profile!')
return render_template('games.html', games=games)
In this two lines of code
ID_GAME = request.form.get('ID_GAME')
NM_GAME = request.form.get('NM_GAME')
You are always getting the data from the first line since you are using get but not gelist.
The way I would do this is to first modify your checkbox
<td>
<input type="checkbox" id="one_checkbox" name="one_checkbox" value="{{ your_game_id }}">
</td>
So that you can get all the games that are checked by their id
print(request.form.getlist('one_checkbox'))
[checked_game_id_1, checked_game_id_2, ...]
Then, use a for loop to loop through the checked games, use their id to get ID_GAME and NM_GAME, and do your processing.
EDIT
To get NM_GAME by ID_GAME
for id in request.form.get('one_checkbox')
game = V_GAMES.query.filter_by(ID_GAME=id).first()
nm_game = game.NM_GAME
Related
Hi i am new to django and html and I am making a shopping CRUD project with models Producst,categories,Sub_categories,size,colors using SERIALIZERS.
I am now trying to make crud of categories,and while inserting I am getting the following error:
"{'category_name': [ErrorDetail(string='Incorrect type. Expected pk value, received str.', code='incorrect_type')]}"
below is my show_cat function
def show_cat(request):
showcategory = Categories.objects.filter(isactive=True)
#print(showall)
serializer = CategoriesSerializer(showcategory,many=True)
#print(serializer.data)
return render(request,'polls/show_cat.html',{"data":serializer.data})
insert_cat function
def insert_cat(request):
if request.method == "POST":
insertcategory = {}
insertcategory['category_name']=request.POST.get('category_name')
insertcategory['category_description']=request.POST.get('category_description')
form = CategoriesSerializer(data=insertcategory)
if form.is_valid():
form.save()
print("hkjk",form.data)
messages.success(request,'Record Updated Successfully...!:)')
return redirect('categories:show_cat')
else:
print(form.errors)
return redirect('categories:show_cat')
else:
return render(request,'polls/insert_cat.html')
below is html for insert
<tr>
<td>Category Name</td>
<td>
<input type="text" name="category_name" placeholder="CATEGORIES">
</td>
</tr>
<tr>
<td>Description</td>
<td>
<textarea name="category_description" id="" cols="30" rows="10">
</textarea>
</td>
</tr>
below is the showcategory html page
{% for result in data %}
<tbody>
<tr>
<td><b>{{result.category_name}}</b></td>
<td><b>{{result.category_description}}</b></td>
<td style="position: relative;left:50px;">
<a href="categories/edit_cat/{{result.id}}">
<button class="btn btn-primary">
<i class="fa-solid fa-pen-to-square">EDIT</i>
</button>
</a>
</td>
<td>
<a href="{% url 'categories:del_cat' result.id %}" onclick="return confirm('Are You Sure you want to delete?')">
<button class="btn btn-danger">
<i class="fa-solid fa-trash">DELETE</i>
</button>
</a>
</td>
</tr>
</tbody>
{% endfor %}
</table>
Where am I going wrong in the code
I'm building an Ecommerce App using Django, I'm storing my cart object in Django Sessions, Add to cart, reduce quanity & increase quantity seem to work perfectly on the products page but the same logic isnt working on the cart page. Django isnt throwing any errors. Below is the snippet of code of my form located inside a table on the Cart page & the view function handling its post request:
FORM:
<div class="container">
<div class="border rounded p-4 m-4">
<p class="display-4 pl-4 ml-4">Your Cart</p>
<hr>
<table class="table">
<thead>
<tr>
<th>Sno.</th>
<th>Image</th>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
<th> </th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr>
<td>{{forloop.counter}}</td>
<td>___</td>
<td>{{product.name}}</td>
<td>{{product.price}}</td>
<td>{{product|cart_quantity:request.session.cart}}</td>
<td>{{product|price_total:request.session.cart}}</td>
<td>
<form action="/cart/#{{product.id}}" method="POST">
{% csrf_token %}
<input hidden type="text" name="product" value="{{product.id}}">
<input hidden type="text" name="remove" value="True">
<input type="submit" value=" * " class="btn btn-block btn-light border-right">
</form>
</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<th colspan="4"></th>
<th class="" colspan="">Total</th>
<th>{{products|total_cart_price:request.session.cart}}</th>
</tr>
</tfoot>
</table>
VIEW FUNCTION:
class Cart(View):
def get(self, request):
ids = list(request.session.get('cart').keys())
products = Product.get_products_by_id(ids)
return render(request , 'cart.html' , {'products' : products} )
def post(self, request):
product = request.POST.get('product')
remove = request.POST.get('remove')
cart = request.session.get('cart')
if remove:
cart.pop(product)
else:
pass
return redirect('cart')
Apparently django sessions object needs to be made aware anytime any changes are to be made to the object inside sessions. The fix is to add :
request.session.modified = True
I'm testing this rn and it seems to work fine, what i dont get is why on the homepage it works without adding the modified command. If anyone has any further knowledge on this issue. Do share
I've been working on a really simple,maybe trivial, flask application that manages users' credentials, like a sysadmin page (change username, password, privileges, email).
Right now I've written the following code in HTML:
<div id="signup-box" class="signup-box" style=" background-color:rgb(149, 202, 202); width: 60%;">
<table class="table" style="background-color:#ffffff00;">
<thead >
<tr>
<th style="text-align:center; vertical-align:inherit;">Username</th>
<th style="text-align:center; vertical-align:inherit;">Role</th>
<th style="text-align:center; background-color:rgba(104, 159, 223, 0.384);">Control</th>
<tr>
</thead>
<tbody>
{% for i in range(0,lenUser) %}
<tr class="tr">
<td style="text-align:center; vertical-align:inherit;">{{ users[i] }}</td>
<td style="text-align:center; vertical-align:inherit;">{{ roles[i] }}</td>
<td style="text-align:center;">
<button form="edit_form" type="submit" value="Edit" class="button is-warning is-focused" style="margin-right: 45px;">Edit User</button>
<button form="delete_form" type="submit" value="Delete" class="button is-danger is-focused" style="margin-right: 45px;">Delete User</button>
<form id="edit_form" action="{{ url_for('edit_user', id=users.id) }}" method="POST">
<input type="hidden" name="_method" value="EDIT">
</form>
<form id="delete_form"action="{{ url_for('delete_user', id=users.id) }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
in Python:
#app.route("/delete_user/")
def delete_user():
if not session.get('logged_in'):
return redirect(url_for('login'))
con = sqlite3.connect('sqlite:///accounts.db')
cur = con.cursor()
cur.execute('DELETE FROM User WHERE id = "' + request.args.get('id')+ '"')
cur.commit()
con.close()
return flask.jsonify({'success':"True"})
And as a Python helper to fetch my users:
def fetch_users():
with session_scope() as s:
usrs = s.query(tabledef.User.username).order_by(tabledef.User.username).all()
tblUsrs = df.from_records(usrs)
return(tblUsrs)
Now, what it does is that it fetches my users as a dataframe and it outputs them row by row in my html table, which is what it was intended to do. But I also want to delete the specific user when I press the button "Delete" in the HTML page, right? Problem is that I can't seem to find a way to select the specific id of the user from the following lines:
<form id="delete_form"action="{{ url_for('delete_user', id=users.id) }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
</form>
How can I fix that? Because it is in a loop and every time the string I get is that of the first line
So I am using Flask as micro framework and in one of my templates I am using the following Table:
<table id = "productentabel" width = "auto" class="table table-striped b-t b-b">
<thead>
<tr class = "header">
<th>Name</th>
<th>Url</th>
</thead>
{% for file in all_files['files'] %}
{% if file['mimeType'] == 'application/vnd.google-apps.document' %}
<TR>
<TD width="auto" >{{file['name']}}</TD>
<td><a class="btn btn-xs white" href = "https://docs.google.com/document/d/{{file['id']}}" target ="_blank">Go to file</a></td>
<td><form method=POST action="delete_file"><input type=submit name="delete" value ="{{file['id']}}" class="btn btn-xs white">Delete file</input>
</form></td>
</TR>
{% endif %}
{% endfor %}
</tr>
</table>
My question is about the following HTML code:
<form method=POST action="delete_file"><input type=submit name="delete" value ="{{file['id']}}" class="btn btn-xs white">Delete file</input>
</form>
As you can see I am trying to pass a value to my Python code when a click is made on the input. The value is passed to my Python code, but now the value is visible on the front end, so it looks like this:
1svpTERHaYd-wSpFBRRRjp1TOj0H-FH4_66H2W1OLY Delete file
But I want it to be like this:
Delete file
In Python I am doing the following to extract the value:
fileid = request.form.get('delete')
I also tried something like this:
<form method=POST action="delete_file"><input type=submit name="{{file['id']" class="btn btn-xs white">Delete file</input>
</form>
But I don't really know how I then can extract the name in my Python code, because I only need file['id'] to be passed and the value solution worked for me but that is not the ideal solution.
Instead of POST try the GET method, like this:
<td><form method="get" action="delete_file?file_name={{file['id']}}"><input type="submit" name="delete" value ="Delete file" class="btn btn-xs white"/></td>
If you want the POST method, you should send the file name via input with the hidden type.
<td><form method="post" action="delete_file"><input type="submit" name="delete" value ="Delete file" class="btn btn-xs white"/><input type="hidden" name="file_id" value="{{file['id']}}" /></td>
In this case you'll get the file id like this:
fileid = request.form.get('file_id')
BTW: most of your HTML isn't valid, you should really watch a tutorial about it.
when click add row button it add new row with two input field but name is different
here is my view
#views.py
def index(request):
#if post request came
if request.method == 'POST':
for i in range(len(request.POST.getlist())):
dd = list(request.POST.getlist('name['+i+']'))
ff = list(request.POST.getlist('email['+i+']'))
context = {
'dataone': dd,
'datatwo': ff,
}
#getting our showdata template
template = loader.get_template('loggedin.html')
#returing the template
return HttpResponse(template.render(context, request))
else:
#if post request is not true
#returing the form template
template = loader.get_template('dynamictabel.html')
return HttpResponse(template.render())
how to get all values of input field in one variable and send to the other page
Here is my main form when click add row button it add new row with two input field but name is different increase with name=name[+1]
#mymainform.html
<table id="tab_logic" name="mytable">
<thead>
<tr >
<th class="text-center">
data1
</th>
<th class="text-center">
data2
</th>
<th class="text-center">
delete
</th>
</tr>
</thead>
<tbody>
<tr id='addr0'>
<td>
<input type="text" name="name[0]" class="form-control"/>
</td>
<td>
<input type="text" name="email[0]" class="form-control"/>
</td>
<td><button class="btn btn-default btn-sm delete">
<span class="glyphicon glyphicon-trash"></span></button></td>
</tr>
<tr id='addr1'>
<td>
<input type="text" name="name[1]" class="form-control"/>
</td>
<td>
<input type="text" name="email[1]" class="form-control"/>
</td>
<td><button class="btn btn-default btn-sm delete"><span
class="glyphicon glyphicon-trash"></span></button></td>
</tr>
</tbody>
</table>