How to transfer data from tables to web forms? - python

I have a table that displays information through a list of dictionaries.
This is the program :
<div class = "col-md-8" >
<table class="table table-bordered table-striped table-hover">
<thead> <!-- En-tĂȘte du tableau -->
<tr>
<th> Host Name </th>
<th> Hardware </th>
<th> Fixed-Address </th>
<th> Comment </th>
<tr>
</thead>
<tbody> <!-- Corps du tableau -->
{% for item in items %}
<tr id="confirm">
{% for key in [ 'host','hardware','fixed_address','comment' ] %}
<td> {{ item[key].decode('utf-8') }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
What I would like to do is to be able to modify it or add a new entry, that's not what I am going to ask here because before that I need to do the following:
Basically, I need to make every row act as a link, when I click on it (the row) I should be redirected to a web page with web forms and those web forms will be filled with the data from the table cells.
How can I do it? Thanks beforehand.

Do something like this.
<thead> <!-- En-tĂȘte du tableau -->
<tr>
<th> Host Name </th>
<th> Hardware </th>
<th> Fixed-Address </th>
<th> Comment </th>
<th> View </th>
<tr>
</thead>
<tbody> <!-- Corps du tableau -->
{% for item in items %}
<tr id="confirm">
{% for key in [ 'host','hardware','fixed_address','comment' ] %}
<td> {{ item[key].decode('utf-8') }}</td>
{% endfor %}
View
</tr>
{% endfor %}
</tbody>
</table>
</div>
That function inside url_for should be the function which displays data and should renders page for displaying data.

Related

HTML Table - Iterate over multiple Lists and Create a Table

I have 3 lists of data that i would like to print out as a Table in HTML.
Lists
ipaddress [10.1.1.0,10.1.1.1,10.1.1.2,10.1.1.3]
State [Full,Full,Full,Full]
Interface [ge0,ge1,ge2,ge3]
ID [ 0,1,2,3]
I would like to Print above rows as a table under Table Column as rows.
Table Header
Address Interface State ID
I cant seem to figure out the proper for loops logic to print this as a table, here is what i have currently.
{%extends "home/layout.html"%}
{% block body%}
<h1 style="text-align:center;">Here is the Inventory </h1>
<li> {{ospfneighboraddress}}</li>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Address</th>
<th scope="col">Interface</th>
<th scope="col">State</th>
<th scope="col">ID</th>
</tr>
</thead>
<tbody>
{% for ospfaddress in ospfneighboraddress %}
<tr>
<td>{{ ospfaddress }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{%endblock%}
ospfneighboraddress is the first IP address list, rest are also lists
Here are other list names:
ospfneighborinterface
ospfneighborstate
ospfneighborID
How do print next three lists for each IP address ?
Here you go!
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table border="1">
<tr>
<th>Address</th>
<th>Interface</th>
<th>State</th>
<th>ID</th>
</tr>
<tr>
<td>10.1.1.0</td>
<td>ge0</td>
<td>Full</td>
<td>0</td>
</tr>
<tr>
<td>10.1.1.1</td>
<td>ge1</td>
<td>Full</td>
<td>1</td>
</tr>
<tr>
<td>10.1.1.2</td>
<td>ge2</td>
<td>Full</td>
<td>2</td>
</tr>
<tr>
<td>10.1.1.3</td>
<td>ge3</td>
<td>Full</td>
<td>3</td>
</tr>
</table>
</body>
</html>

Django: How to add html tag from string to the html template

Details:
Client framwork: Django
Web server: Python + Flask
I have an HTML page which rendered with a data array.
Each item in the array contains some fields. And one of the fields is a json object.
In order to improve the visibility of the json i've converted the json obj to html table.
Currently it's does't work.
The results (on the browser) looks like that:
PasswordPolicy <table border="1"><tr><th>MinimumPasswordLength</th><td>16</td></tr><tr><th>RequireSymbols</th><td>True</td></tr><tr><th>Re
Flask code:
#app.route("/PasswordPolicy", methods=["GET"])
#login_required
def get_password_policy():
get_table_content = list(db_object.get_password_policy())
search_event = request.args.get('searchquery')
at_type = request.args.get('audittype')
print("**************")
for e in get_table_content:
print("Before:")
print(e['PasswordPolicy'])
e['PasswordPolicy'] = json2html.convert(json = e['PasswordPolicy'])
print("After:")
print(e['PasswordPolicy'])
print("get_table_content:")
print(get_table_content[0])
print("**************")
return render_template(
'soc2/password_policy.html',
get_list=get_table_content
)
Html content:
<table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
<thead>
<tr class="headings">
<th class="column-title">BusinessUnit </th>
<th class="column-title">ControllerNumber </th>
<th class="column-title">PasswordPolicy </th>
</tr>
</thead>
<tbody>
{% for elements in get_list %}
<tr class="even pointer">
<td>
{{ elements["BusinessUnit"] }}
</td>
<td>
{{ elements["ControllerNumber"] }}
</td>
<td>
{{ elements["PasswordPolicy"] }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
The question is how to tell to the browser to parse the table as html instead of simple string
You can use safe like so: {{ your_html_string | safe }}

How to create dynamic header with vertical table in Django template

I have data:
data =
[
{
"name":"bolb",
"category_name":"Electronics",
"size":"34",
"price":"890",
"currency_name":"Us",
},
{
"name":"bolb",
"category_name":"Electronics",
"size":"2",
"price":"9099",
"currency_name":"Us",
}
]
I need to show this data into template like as below.
<table>
<tbody>
<tr>
<td>Category Compare</td>
<td>bolb</td>
<td>bolb asun</td>
</tr>
<tr>
<td>price </td>
<td>890</td>
<td>9099</td>
</tr>
<tr>
<td>category_name</td>
<td>Electronics</td>
<td>Electronics</td>
</tr>
<tr>
<td>currency_name</td>
<td>Us</td>
<td>Us</td>
</tr>
</tbody>
</table>
My desire output table vertically with dynamic. I have added image also for clarification. Can i change my variable data or i can do it using for loop.Please suggest best idea or code will be appreciate.
This is my output image:
you may do something like this:
<table>
<tbody>
<tr>
<td>Category Compare</td>
{% for i in data %}
<td>{{i.name}}</td>
{% endfor %}
</tr>
<tr>
<td>price </td>
{% for i in data %}
<td>{{i.price}}</td>
{% endfor %}
</tr>
<tr>
<td>category_name</td>
{% for i in data %}
<td>{{i.category_name}}</td>
{% endfor %}
</tr>
<tr>
<td>currency_name</td>
{% for i in data %}
<td>{{i.currency_name}}</td>
{% endfor %}
</tr>
</tbody>
</table>

pass list of values from html button to python scripts

i have a html template like:
<button id="demo-btn-" >Pack</button>
<table class="table table-striped">
<thead>
<tr>
<th><input type="checkbox" onClick="toggle(this, 'no')"></th>
<th>Invoice</th>
<th>User</th>
<th>Order date</th>
<th>Amount</th>
<th>Status</th>
<th>Tracking Number</th>
</tr>
</thead>
<tbody>
{% for oid,dbd,stts,tp in new_orders %}
<tr>
<td><input type="checkbox" name="no" value="{{oid}}"></td>
<td><a class="btn-link" href="#">{{oid}}</a></td>
<td>Steve N. Horton</td>
<td><span class="text-muted"><i class="fa fa-clock-o"></i>{{dbd}} </span></td>
<td>{{tp}}</td>
<td>{{stts}}</td>
<td>-</td>
</tr>
{% endfor %}
</tbody>
</table>
when i select the checkbox in <thead> all the checkboxes in <tbody> are selected. Now i want, when i select the checkbox and press the pack button, the oid value for each row should be passed to a django view. How to do that?
EDIT:
I want to pass multiple oids as a list to python script. I can do that if i can pass these as a list to django app's views file. I dont know how to pass a list or a value to a django application.

mustache, babel and gettext

I am using Flask, jinja together with Mustachjs.
In order to get the job done I am using the {% raw %} tag.
Now, it is a multi-languages application and I use Babel.
How can I do :
{% raw %}
<script id="details" type="text/template">
<table class="table" >
<thead>
<tr>
<th>**{{gettext('col1')}}</th>
<th>**{{gettext('col2')}}</th>
<th>**{{gettext('col6')}}</th>
</tr>
</thead>
<tbody>
{{#skyrsla}}
<tr>
<td> {{index}}</td>
<td> {{nafn}}</td>
<td> {{mean_growth_index}}</td>
</tr>
{{/skyrsla}}
</tbody>
</table>
</script>
{% endraw %}
Since it is between raw tags, the Babel extension does not detect {{gettext('col1')}
Is there a way to alter the configuration of Babel.
My actual configuration looks like :
[python: **.py]
[jinja2: **/templates/**.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_
Simply end your raw blocks between calls to gettext:
{% raw %}
<script id="details" type="text/template">
<table class="table" >
<thead>
<tr>
<th>**{% endraw %}{{gettext('col1')}}{% raw %}</th>
<th>**{% endraw %}{{gettext('col2')}}{% raw %}</th>
<th>**{% endraw %}{{gettext('col6')}}{% raw %}</th>
</tr>
</thead>
<tbody>
{{#skyrsla}}
<tr>
<td> {{index}}</td>
<td> {{nafn}}</td>
<td> {{mean_growth_index}}</td>
</tr>
{{/skyrsla}}
</tbody>
</table>
</script>
{% endraw %}

Categories