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
Related
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!
In my django template code, I've written code for table in nested for loops. By using with statement, I want to put serial number. But it is not working. Have a look at my code in html file:
<tbody>
{% for a_testcases in testcases %}
{% for a_test in a_testcases %}
{% with x=forloop.parentcounter.counter %}
<tr>
<td id="sno_{{a_test.id}}">{{ forloop.counter|add:x }}</td>
<td id="name_{{a_test.id}}">{{ a_test.name }}</td>
<td id="cputime_{{a_test.id}}">{{ a_test.safe_exec.cpu_time }}</td>
<td id="clocktime_{{a_test.id}}">{{ a_test.safe_exec.clock_time }}</td>
<td id="memorylimit_{{a_test.id}}">{{ a_test.safe_exec.memory }}</td>
<td id="stacksize_{{a_test.id}}">{{ a_test.safe_exec.stack_size }}</td>
<td id="child_{{a_test.id}}">{{ a_test.safe_exec.child_processes }}</td>
<td id="openfiles_{{a_test.id}}">{{ a_test.safe_exec.open_files }}</td>
<td id="maxfilesize_{{a_test.id}}">{{ a_test.safe_exec.file_size }}</td>
</tr>
{% endwith %}
{% endfor %}
{% endfor %}
</body>
It is showing nothing in place of serial number.How to fix it?
I'm working with Flask and MySQL to create a table that gets populated based on the database. Now, when a user clicks on a particular row, I need to redirect to another page that will tell me more about that particular row.
I have the redirection part down but how do I get information about that row on the backend?
Here is my HTML code:
<tbody>
{% for row in resultPois %}
<tr onclick="window.document.location='/pageDetail';">
<td>{{ row[0] }}</td>
<td>{{ row[1] }}</td>
<td>{{ row[2] }}</td>
<td>{{ row[3] }}</td>
<td>{{ row[4] }}</td>
<td>{{ row[5] }}</td>
</tr>
{% endfor %}
</tbody>
On the flask side:
#app.route('/pageDetail')
def page_detail():
# get info here to render page!
return render_template('page_detail.html')
You can try flask.url_for(endpoint, **values) when you want pass the variable from template to the backend.
In Flask template:
<tbody>
{% for row in resultPois %}
<tr onclick="window.location.href='{{ url_for('page_detail', rowData=row) }}';">
<td>{{ row[0] }}</td>
<td>{{ row[1] }}</td>
<td>{{ row[2] }}</td>
<td>{{ row[3] }}</td>
<td>{{ row[4] }}</td>
<td>{{ row[5] }}</td>
</tr>
{% endfor %}
</tbody>
In Flask view:
#app.route('/pageDetail/<rowData>')
def page_detail(rowData):
# you can use the the rowData from template
return render_template('page_detail.html'
BTW, What I suggest to design the front end part, better make button as one column in the table row, when click the button, then trigger the onclick function to call url_for, that will be more user friendly.
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>
I am trying to create HTML tables from data stored in a table. My data is read from a table and converted into a dict of lists, e.g.:
x = {'date':[u'2012-06-28', u'2012-06-29', u'2012-06-30'], 'users': [405, 368, 119]}
My goal is to create an HTML table with the following structure for an arbitrary list length:
<table>
<thead>
<th>Date</th>
<th>Users</th>
</thead>
<tbody>
<tr>
<td>2012-06-28</td>
<td>405</td>
</tr>
<tr>
<td>2012-06-29</td>
<td>368</td>
</tr>
<tr>
<td>2012-06-30</td>
<td>119</td>
</tr>
</tbody>
</table>
I have tried doing this two incorrect ways in my Flask template:
<tbody>
{% for line in x %}
<tr>
<td>{{ x.date|tojson|safe }}</td>
<td>{{ x.users }}</td>
</tr>
{% endfor %}
</tbody>
Which prints the entire list into each column.
And:
{% for date in x.date %}
<tr><td>{{ date|tojson|safe }}</td></tr>
{% endfor %}
{% for users in x.users %}
<tr><td>{{ users }}</td></tr>
{% endfor %}
Which simply prints everything into the first column.
These experiments and many other dead ends lead me to believe that there is no simple way to build the table as I would like given my current data structure.
Given this, I have two questions:
1) How would I go about building the table using my current data structure?
2) What is the standard or ideal way to structure data for this use case?
Thanks in advance.
Like you said, you could either change your data structure, or change your template code. Here is one way to keep the current structure:
{% for row_index in range(x['date']|count) %}
<tr>
<td>{{ x[row_index]['date']|tojson|safe }}</td>
<td>{{ x[row_index]['users'] }}</td>
</tr>
{% endfor %}
Or you could restructure your data in python:
x = zip(x['date'], x['users'])
And then use this template:
{% for row in x %}
<tr>
<td>{{ row[0]|tojson|safe }}</td>
<td>{{ row[1] }}</td>
</tr>
{% endfor %}
You can also structure the data so that the template does not depend on the order of the cells:
from itertools import izip
x = [dict(date=d, user=u) for d, u in izip(x['date'], x['users'])]
Then you can access your data like this:
{% for row in x %}
<tr>
<td>{{ row['date']|tojson|safe }}</td>
<td>{{ row['user'] }}</td>
</tr>
{% endfor %}
You might use Flask-Table or for something more complex even leverage Flask-Admin.
Yeah, you really want to use a list of dictionaries instead of a dictionary of lists, that works out better with Jinja2