I have this table in Flask:
<tbody>
{% for record in records %}
<tr>
<td onclick=search_results()>{{ record.id }}</td>
<td>{{ record.contractor }}</td>
<td>{{ record.category }}</td>
<td><A HREF='tel:{{ record.phone }}'>{{ record.phone }}</A></td>
</tr>
{% endfor %}
</tbody>
And this function:
#app.route('/detail/<id>')
def search_results(id):
records = db.session.query(Feedback).filter(Feedback.id == id)
print('search')
for record in records:
print(record)
return render_template('pageDetail.html', records=records)
I have researched similar questions but struggling still to understand how I can pass the id number of the selected cell to the search_results function.
Thank you in advance!
You need to hit the endpoint from the html and not the function call, try doing this,
<tbody>
{% for record in records %}
<tr>
<td href='/detail/{{ record.id }}'>{{ record.id }}</td>
<td>{{ record.contractor }}</td>
<td>{{ record.category }}</td>
<td><A HREF='tel:{{ record.phone }}'>{{ record.phone }}</A></td>
</tr>
{% endfor %}
</tbody>
Your flask view is fine.
No Method has been specified for flask?
If not used then why not simply use a python function.
refer this example:
#app.route('/classify', methods=['GET','POST'])
# these requests trigger the getresponse function where data can be retrieved
def getresponse():
if request.method == 'GET':
return(render_template('main.html'))
if request.method == 'POST':
extracted_title = request.form['string']
try:
#app.route('/detail/<id>', methods=['GET'])
def search_results(id):
records = db.session.query(Feedback).filter(Feedback.id == id)
print('search')
for record in records:
print(record)
return render_template('pageDetail.html', records=records)
Related
I have Quart App with Telegram Telethon, I have a function that returns some data, but the function takes 10-300 seconds. Is there any way to return the page and then wait for the function's data?
QuartApp code:
#app.route("/exporter", methods=["GET", "POST"])
async def exporter():
all_data = []
for i in data:
doingsomething here then append the data
return await render_template('mypage.html', user_data=all_data)
And here is the HTML code:
<tbody>
{% for data in user_data %}
<tr>
<td>{{ data[0] }}</td>
<td>{{ data[1] }}</td>
<td>{{ data[2] }}</td>
<td>{{ data[3] }}</td>
<td>
<div class="form-check d-flex justify-content-center">
<input class="form-check-input" type="radio" name="flexRadioDefault"
value="flexRadioDefault-{{data[2]}}"></div>
</td>
</tr>
{% endfor %}
</tbody>
I'm expecting to load the page while function the doing something in the background
I am trying to get url parameters like this:
<tbody>
{% for object in objects %}
<tr>
<td>{{ object.id }}</td>
<td>{{ object.user }}</td>
<td>{{ object.url }}</td>
<td>{{ object.minimum }}</td>
<td>{{ object.maximum }}</td>
<td>{{ object.requests }}</td>
<td>{{ object.stay }}</td>
<td class="text-Centre">
Run
</td>
</tr>
{% endfor %}
</tbody>
If I put my URL like this, it works since the view exists in view.py:
{% url 'trafficapp:generate_traffic' %}
Now, I am trying to take URL parameters with it, but I am unable to make how can I pass data from this button, and what should I put in my URL pattern for this. Kindly help, I am new to Django and still figuring out how it works.
you can add parameters after {% url %}
Run
I created an html table in a django template
<form action="/delete_team/" method="POST" >
{% csrf_token %}
<input type="submit" name = "delete" class="btn btn-danger float-right" value="Delete">
<table>
<thead>
<tr>
<th><input type="checkbox" class="checkAll" name="checkAll"></th>
<th>Team ID</th>
<th>Male</th>
<th>Female</th>
<th>Officer</th>
<th>Deadline</th>
</tr>
</thead>
<tbody>
{% for team in teams %}
<tr>
<td><input type="checkbox" name="checked" class="case"></td>
<td>{{ team.team_id}}</td>
<td>{{ team.male }}</td>
<td>{{ team.female }}</td>
<td>{{ team.officer }}</td>
<td>{{ team.date }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
here is the code that i wrote in my views.py:
def delete_team(request):
if request.method == "POST":
pkm = request.POST.getlist("checked")
selected_objects = mtch_tbl.objects.filter(team_id__in=pkm)
selected_objects.delete()
return HttpResponseRedirect("/main/")
now when i check a row and click delete nothing happens...i only get returned to my page again with the same data. Kindly point out my mistake, i can't figure out how to write the views
Here are my urls.py
from django.urls import path
from teamplanner import views
urlpatterns = [
...
path("delete_team/",views.delete_team),
]
Is it possible for the jQuery code to be interfering?
Your views have many codes that to me aren't necessary, if you're planning on deleting a team, it's just Normal that you've FIRST registered a team. So here's what you should do..
def delete_team(request,delete_id):
delete = mtch_tbl.objects.get(id=delete_id)
#to make it more interesting, you should add a view to your models (i,'ll display the model below)
delete.view=True
delete.delete()
return HttpResponseRedirect("/main/")
Add this to your models
Class modelname (models.Model):
view=models.BooleanField(default=False)
In your URLs here's what you should do:
urlpatterns = [
path("delete_team/(?<delete_id>\d+)/$",views.delete_team),
]''''
I hope this helps
I think you're storing it in a variable. Try this:
def delete_team(request):
if request.method == "POST":
pkm = request.POST.getlist("check")
mtch_tbl.objects.filter(team_id__in=pkm).delete()
return HttpResponseRedirect("/main/")
I figured out the answer after reading this S.O solution, it's not related to checkboxes but the idea is pretty much the same. All I did was edit my checkbox input tag a little and added values to it:
<tbody>
{% for team in teams %}
<tr>
<td><input type="checkbox" name="checked" class="case" value="{{ team.team_id }}"></td>
<td>{{ team.team_id}}</td>
<td>{{ team.male }}</td>
<td>{{ team.female }}</td>
<td>{{ team.officer }}</td>
<td>{{ team.date }}</td>
</tr>
{% endfor %}
</tbody>
that's all there was to it, no need to change the views or the url path.
Still I really appreciate the help, Thank You!
I'm trying my best not to repeat myself in my code but I'm encountering a problem looping through a dictionary by key in my template.
I have two dicts:
exampledict={'firstkey':firstval, 'secondkey':secondval}
keys=['firstkey', 'secondkey']
keydict={'keys':keys}
In my template I want to loop over the exampledict using the keydict:
<tr>
{% for val in keydict %}
<td>{{ exampledict.val }}</td>
{% endfor %}
</tr>
I've noticed this kind of combination of variables doesn't work at all, I tried by using:
{'firstkey':'firstkey'}
And sending that through to the template and later trying
{{ exampledict.firstkey }}
Is there a better way to accomplish what I'm trying to do here?
Edit 1:
Manually going through each key as:
<td> {{ exampledict.firstkey }} </td> <td> {{ exampledict.secondkey }} </td>
Where firstkey and secondkey is the actual dictkey for exampledict works, although it makes for a lot of repetition.
Edit 2:
views.py
def tabletest(request):
exampledict={'firstkey':'firstval', 'secondkey': 'secondval'}
keydict={
'keys':['firstkey', 'secondkey']
}
return render(request, 'MinaFakturor/tabletest.html', {'exampledict':exampledict, 'keydict':keydict})
template
<table>
<thead>
<tr>
{% for val in keydict.keys %}
<th>{{ val }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
<tr>
{% for val in keydict.keys %}
<td>{{ exampledict.val }}</td>
{% endfor %}
</tr>
<tr>
<td>{{ exampledict.firstkey }}</td>
</tr>
</tbody>
</table>
Which produces this result:
If I remove the exampledict.firstkey term, nothing is produced in the table body.
I'm trying to parse the following data structure in my HTML.
{'GROUPS': {'Group1': [{'key1':'value1','key2':'value2'}, {'key1':'value3', 'key2':'value4'}], 'Group2': [{'key1':'value5','key2':'value6'}, {'key1':'value7', 'key2':'value8'}]}}
The parsing code that I have is as follows:
<tbody>
{% for group,data in data|get_value:"GROUPS" %}
<tr>
<td>{{ group }}</td>
{% for v in data.items %}
<tr>
<td>{{ v|get_value:"key1" }}</td>
<td>{{ v|get_value:"key2" }}</td>
</tr>
{% endfor %}
</tr>
{% endfor %}
</tbody>
get_value is the custom filter that I've written which basically takes the key and the data structure, and returns the value back.
But this isn't working. Can anyone help me figure out why? Thanks!
Firstly, for constant keys, you don't need a custom filter, this will work just fine:
{{ v.key1 }}
That said, data['GROUPS'] is a dict, and you want to iterate over its items, like you did with data.
data is a list though and doesn't need that:
<tbody>
{% for group, data in data.GROUPS.items %}
<tr>
<td>{{ group }}</td>
{% for v in data %}
<tr>
<td>{{ v.key1 }}</td>
<td>{{ v.key2 }}</td>
</tr>
{% endfor %}
</tr>
{% endfor %}
</tbody>