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
Related
I am new to both django and web development.
The task is to run a script when pressin a button using the data contained in the specific row of an html table.
So if i clik on the second button "Run script" it uses all the data in that row (8888, 2020/06/21 06:00) in a separate script to performe some task.
Currently my html file looks like this:
There are 2 sections one for uplading the information that goes in the table one the table which displays them
<h1>Approach Path KML Generator</h1>
<h2>Generate the KML file here:</h2> <form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button onclick="location.href='{% url 'approach_path_receptorsKML:Proejct Creator' %}'">Upload</button> </form>
<h2>Projects Available:</h2>
<table class="table">
<thead>
<tr>
<th>Project ID</th>
<th>Date KML</th>
<th>Time KML</th>
<th>Date-Time Uploaded</th>
<th>Run The Conversion</th>
<th>KML File</th>
</tr>
</thead>
<tbody>
{% for project in latest_project_list %}
<tr>
<td>{{ project.project_ID }}</td>
<td>{{ project.date_kml }}</td>
<td>{{ project.time_kml }}</td>
<td>{{ project.upload_time }}</td>
<td>
<button method="post" value="collect data" name="{{ project.project_ID }}|{{ project.date_kml }}|{{ project.time_kml }}|{{ project.upload_time }}">Run script</button>
</td>
<td>
Download KML File
</td>
</tr>
{% endfor %}
</tbody> </table>
And this is the view I have created:
def ProjectCreator(request):
form = DocumentForm()
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid:
form.save()
elif 'collect data' in request.POST.values():
values = [key for key in request.POST.keys()]
print(values)
else:form = DocumentForm()
I have tried to use this guide (How do I pass table data from a template over to Django on a button/submit click?) however, I have been insuccesfull.
If anyone can spot the mistake I have made and give me some explanation I would be grateful.
Thanks
It doesn't work because you have incorrect HTML layout. Button itself doesn't do anything - in order to send POST request, it should be in <form> tag. Try following:
{% for project in latest_project_list %}
<tr>
<td>{{ project.project_ID }}</td>
<td>{{ project.date_kml }}</td>
<td>{{ project.time_kml }}</td>
<td>{{ project.upload_time }}</td>
<td>
<form method="post">
{% csrf_token %}
<button value="collect data"
name="{{ project.project_ID }}|{{ project.date_kml }}|{{ project.time_kml }}|{{ project.upload_time }}">
Run script
</button>
</form>
</td>
<td>
Download KML File
</td>
</tr>
{% endfor %}
This will work, but I doubt this is great way of achieving this. You can just send project.pk with POST request and fetch project in view. This way you can be sure user will not send incorrect/malicious data with request. It is especially important since your code will run script based on data.
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)
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 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>