I would like to change the content of the row divs inside the indices_container when the screen becomes small. Currently, on all screen sizes, each row shows the index name, price, 1-day change, and ytd change. I would only like to show the index name and ytd change on small screens.
I am only using Flask and bootstrap to serve my application so I am not even sure if this is even possible without something like react and vue. Can this be done with #media or browser JS?
index.html file:
{% extends 'base.html' %}
{% block body %}
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
{{message}}
</div>
{% endfor %}
{% endif %}
{% endwith %}
<div class="indices-container">
<div class="row">
{% for index in total_payload.get('indexes').get('meta_data') %}
<div class="col-sm-2 text-center">
{% set d = total_payload.get('indexes').get(index) %}
{{ "{} ${:.2f}".format(index, d['current_price']) }}
{{ "{:0.2f}% 1-Day".format(d['percent_change']) }}
{{ "{:0.2f}% YTD".format(d['ytd_percent_change']) }}
</div>
{% endfor %}
</div>
</div>
<div class="sp500-table-container">
<div class="table-responsive" id="sp500Table">
<table class="table table-light table-striped table-sm" id="sp500Table">
<thead class="thead-light">
<tr> {% for col in total_payload.get('companies').get('meta_data') %}
<th>{{ col }}</th>
{% endfor %}
</tr>
</thead>
<tbody> {% for num, r in enumerate(total_payload.get('companies').get('sp500companies')) %}
<tr>
<td>{{ num+1 }}</td>
<td>{{ r.get('Symbol') }}</td>
<td>{{ r.get('Security') }}</td>
<td>link</td>
<td>{{ r.get('Sector') }}</td>
<td>{{ r.get('SubSector') }}</td>
<td>{{ r.get('Headquarters') }}</td>
<td>{{ r.get('DateFirstAdded') }}</td>
</tr> {% endfor %}
</tbody>
</table>
</div>
</div>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p.name-price").css("color", "red");
});
});
</script>
{% endblock %}
style.css file:
html {
font-family: Arial, Helvetica, sans-serif;
}
body {
/* padding-bottom: 80px; */
/* height: 100% */
}
main {
/* display: flex; */
/* flex-direction: column; */
/* flex-grow: 1; */
padding-bottom: 80px;
overflow: hidden;
}
footer {
padding-top: 80px;
}
.indices-container {
margin: auto;
}
.sp500-table-container {
height: 70vh;
width: 100vw;
overflow: auto;
position: relative;
padding-bottom: 16px;
margin: auto;
/* flex: 1; */
/* margin-bottom: 16px; */
}
This is done with css native Media Queries, no additional libraries needed.
It would look something like this:
#media (max-width: 400px){
.indicie-payment { display: none; }
...
}
BUT
Since you said you are already using Bootstrap, you can use some utility classes for display that are already provided by Bootstrap
In your case you would need to wrap some of your items in an element like div or span and use a utility class like d-sm-none
So it might look something like this:
<div class="indices-container">
<div class="row">
{% for index in total_payload.get('indexes').get('meta_data') %}
<div class="col-sm-2 text-center">
{% set d = total_payload.get('indexes').get(index) %}
<span class="d-sm-none">{{ "{} ${:.2f}".format(index, d['current_price']) }}</span>
<span class="d-sm-none">{{ "{:0.2f}% 1-Day".format(d['percent_change']) }}</span>
{{ "{:0.2f}% YTD".format(d['ytd_percent_change']) }}
</div>
{% endfor %}
</div>
</div>
Related
this is my code i want to give different colors to comments
like grey(#eee) to odd and blue (#e6f9ff) to even
here this line is giving me error
background-color: {{ bg_color }}
{% extends 'base.html' %}
{% block content %}
<h1>{% block title %} Comments {% endblock %}</h1>
<div style="width: 50%; margin: auto">
{% for comment in comments %}
{% if loop.index % 2 == 0 %}
{% set bg_color = '#e6f9ff' %}
{% else %}
{% set bg_color = '#eee' %}
{% endif %}
<div style="padding: 10px; background-color: {{ bg_color }}; margin: 20px">
<p>#{{ loop.index }}</p>
<p style="font-size: 24px">{{ comment }}</p>
</div>
{% endfor %}
</div>
{% endblock %}
I think jinja version is updated and my course which Im following is old
so anyone who knows to answer me
You should note the scope for the variables. If you define a variable within an if-else block, it is only valid there.
<div style="width: 50%; margin: auto">
{% for comment in comments -%}
{% set bg_color = '#e6f9ff' if loop.index % 2 == 0 else '#eee' %}
<div style="padding: 10px; background-color: {{ bg_color }}; margin: 20px">
<p>#{{ loop.index }}</p>
<p style="font-size: 24px">{{ comment }}</p>
</div>
{% endfor -%}
</div>
Instead of using jinja you can also use css to assign a different background color to every other line.
<div class="comments">
{% for comment in comments -%}
<div>
<p>#{{ loop.index }}</p>
<p class="text">{{ comment }}</p>
</div>
{% endfor -%}
</div>
<style type="text/css">
.comments {
width: 50%;
margin: auto;
}
.comments div {
padding: 10px;
background-color: #e6f9ff;
margin: 20px
}
.comments div:nth-child(odd) {
background-color: #eee;
}
.comments .text {
font-size: 24px
}
</style>
I tried some of the answers posted on this forum but cannot find one fitting for me,
I realise this is an age old question sorry.
Im trying to get a vertical line like in this design:
The problem i'm facing is that this is in a table and I cannot figure out how to get them to cros like this.
Its about the element td with 'scores' id
Twig file
{% extends 'base.html.twig' %}
{% block body %}
<div class="table-responsive">
{% for group in duel_groups %}
{% if group is not empty %}
<table class="table table-bordered table-light" style="margin-top: 30px;">
<thead>
<tr>
<th>Omloop</th>
<th>Partuur 1</th>
<th>Scores</th>
<th>Partuur 2</th>
{# <th>Spelers</th>#}
</tr>
</thead>
<tbody class="text-center">
{% for duel in group %}
<tr>
<td>{{ duel.omloopNummer }}</td>
<td id="team1">{{ duel.team1 }}</td>
<td id="scores">
{{ duel.eerstenP1 }} {{ duel.eerstenP2 }}<br>
<hr>
{{ duel.puntenP1 }} {{ duel.puntenP2 }}
</td>
<td id="team2">{{ duel.team2 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% endfor %}
</div>
{% endblock %}
I have tried creating a div of 1px but that didn't work.
I tried a couple of solutions from this question How to make a vertical line in HTML
None seem to fit my use case tho.
Poking around some more came up with this:
<td id="scores" style="background: linear-gradient(#bcbcbd, #bcbcbd) no-repeat center/3px 85%;">
{{ duel.eerstenP1 }} {{ duel.eerstenP2 }}<br>
<hr style="border-top: 3px solid #000000; background: transparent;">
{{ duel.puntenP1 }} {{ duel.puntenP2 }}
</td>
Where I am adding a vertical line to the middle of the column and styling the hr tag
How do I prevent the content of a long table or list from displaying over my footer if it takes up more than 100% of the height of the screen? I'd like the table to be responsive and take up the entire div. I've tried this answer but manually setting the bottom-margin did not help. My current ad-hoc solution is to manually limit the table height to prevent if from spilling over the footer. Below is the index.html that extends from base.html
/* style.css */
html {
font-family: Arial, Helvetica, sans-serif;
}
header {
padding-bottom: 16px;
}
body {
padding-bottom: 120px;
}
div#sp500Table.table-responsive {
overflow-y: scroll;
height: 500px;
}
<!-- Bootstrap 4 -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<!--base.html -->
<body class="bg-dark text-white container-fluid">
<header class="navbar navbar-expand-sm navbar-dark sticky-top bg-dark">
...
</header>
<main role="main">
<div class="container-fluid d-flex">
{% block body %} {% endblock %}
</div>
</main>
<footer class="page-footer font-small fixed-bottom">
<div class="container">
...
</div>
</footer>
</body>
<!-- index.html -->
{% extends 'base.html' %} {% block body %}
<div class="table-responsive" id="sp500Table">
<table class="table table-light table-striped table-sm" id="sp500Table">
<thead class="thead-light">
<tr> {% for col in table_header %}
<th>{{ col }}</th>
{% endfor %}
</tr>
</thead>
<tbody> {% for num, r in enumerate(payload) %}
<tr>
<td>{{ num+1 }}</td>
<td>{{ r.get('symbol') }}</td>
<td>{{ r.get('security') }}</td>
<td>link</td>
<td>{{ r.get('sector') }}</td>
<td>{{ r.get('sub_sector') }}</td>
<td>{{ r.get('headquarters') }}</td>
<td>{{ r.get('date_first_added') }}</td>
</tr> {% endfor %}
</tbody>
</table>
</div>
{% endblock %}
Put the table into a div and add a height to the div. The add overflow: auto to the div. This will prevent table from taking up more space than you want and still allow you to scroll through all the page content.
I have a base form which contains fields to be filled in by the user and a button which allows the user to add up to ten instances of a different form that is relevant to the base form (this is just dynamically adding forms through django formset). When the html is loaded the base form and all content looks great, but when I go to add a form from the formset, the CSS gets messed up because the form from the formset will neither conform to the base form CSS or any CSS I've added to it.
I've made a div to contain all forms added via the formset add button so that I can format all added forms with one CSS id. However, it seems no formatting works - the added forms' elements expand the base form's margins and seem to be floated.
HTML:
<div class="center-text jumbotron">
<h2>Incident Report Form</h2>
<form method="post" class="form-horizontal">
{% crispy incident_form %}
<div id="form_set_class">
{{ incident_formset.management_form }}
<table>
{% for form in incident_formset %}
{{form.non_filed_errors}}
{{form.errors}}
{% crispy form %}
{% endfor %}
</table>
</div>
<input type="button" id="add_def_report" value="Add Report">
<div id="empty_form" style="display:none">
{{incident_formset.empty_form}}
</div>
<input type="submit" class="btn btn-primary" value="Submit">
</form>
</div>
CSS:
.center-text{
display: block;
text-align: center;
}
form{
display: inline-block;
margin-right: auto;
margin-left: auto;
text-align: left;
}
legend {
float: left; /*allows for top margin */
border-bottom: 1px solid black;
margin-bottom: 20px;
margin-top: 20px;
}
#form_set_class{
clear: both;
display: block;
text-align: center;
}
Good Styling (image shows top part of form):
Messed Up Styling (image shows bottom part of form; the added formset starts at the "Supplier" dropdown):
I presume you have a parent and child model scenario. If that is so, then you may layout your child forms like this (laid out in tabular fashion):
<!-- ABOVE THIS WOULD BE YOUR PARENT FORMS -->
<!-- HERE WE CAN HAVE TABLE HEADER FOR THE FORMS ADDED DYNAMICALLY -->
<table class="table table-bordered table-striped table-sm">
{{ incident_formset.management_form }}
{% for form in incident_formset.forms %}
{% if forloop.first %}
<thead id="hdrID">
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<!-- HERE WILL BE THE FORMS ADDED BY THE USER -->
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
{% endfor %}
</table>
<!-- BELOW THIS WOULD BE THE FORM SUBMISSION AND THE REST -->
You may then think of applying css as per your requirements using tags (eg. hdrID shown in the code).
I am using the http://www.highcharts.com/demo/gauge-speedometer on a page that dynamically renders multiple gauges. The gauges are grouped by their current level. I only want a maximum of 6 gauges in a row with any additional gauges to display on a new row.
The gauges are all working properly. However, if there are 7 gauges with a level of "red", they are flowing off the page.
My view returns a "gauge_list" which is a list of dictionaries for each gauge like this:
{'cnt': 48, 'yellow_to': 66, 'level': 'yellow', 'gauge_min': 0, 'gauge_link': 'gauges:contracts', 'green_to': 33, 'gauge_max': 100, 'gauge_title': 'contracts', 'gauge_name': 'Contracts'}
Any suggestions on the best way to handle this?
<div class="row">
<div class="span12">
<table>
{% for gauge in gauge_list %}
{% if gauge.level == "red" %}
<td><div id="{{gauge.gauge_title}}" style="width: 280px; height: 210px; margin: 0 auto"></div></td>
{% endif %}
{% endfor %}
</table>
</div>
</div>
<div class="row">
<div class="span12">
<table>
{% for gauge in gauge_list %}
{% if gauge.level == 'yellow' %}
<td><div id="{{gauge.gauge_title}}" style="width: 280px; height: 210px; margin: 0 auto"></div></td>
{% endif %}
{% endfor %}
</table>
</div>
</div>
<div class="row">
<div class="span12">
<table>
{% for gauge in gauge_list %}
{% if gauge.level == 'green' %}
<td><div id="{{gauge.gauge_title}}" style="width: 280px; height: 210px; margin: 0 auto"></div></td>
{% endif %}
{% endfor %}
</table>
</div>
</div>
I think the simplest way to do this is to either a) determine the grouped lists in the view or b) use the built-in groupby template tag. groupby requires that the list of dictionaries be arranged by the grouping key, which would be level here. That's best done in the view, since you probably don't want the naive alphabetic sorting that the dictsort filter would give you.
{% regroup gauge_list by level as level_list %}
{% for level in level_list %}
<div class="row">
<div class="span12">
<table>
{% for gauge in level.list %}
{% if forloop.counter0|divisibleby:"6" %}
{% if not forloop.first %}</tr>{% endif %}
<tr>
{% endif %}
<td><div id="{{gauge.gauge_title}}" style="width: 280px; height: 210px; margin: 0 auto"></div></td>
{% endfor %}
</tr>
</table>
</div>
</div>
{% endfor %}
By doing it in the view, I mean something like providing a nested data structure instead of a simple list. Something like:
levels_and_gauges = [['red', [...red guage dicts]],
['yellow', [...yellow guage dicts]],
['green'], [...green guage dicts]]]
The template code would then be something like this:
{% for level, guages in levels_and_guages %}
<div class="row">
<div class="span12">
<table>
{% for guage in guages %}
{% if forloop.counter0|divisibleby:"6" %}
{% if not forloop.first %}</tr>{% endif %}
<tr>
{% endif %}
<td><div id="{{gauge.gauge_title}}" style="width: 280px; height: 210px; margin: 0 auto"></div></td>
{% endfor %}
</tr>
</table>
</div>
</div>
{% endfor %}
Or you could group into 6-length rows in the view rather than the template.
{% for level in ["red", "yellow", "green"] %}
<div class="row">
<div class="span12">
<table>
{% for gauge in gauge_list %}
{% if gauge.level == level %}
<td><div id="{{gauge.gauge_title}}" style="width: 280px; height: 210px; margin: 0 auto"></div></td>
{% endif %}
{% endfor %}
</table>
</div>
</div>
{% endfor %}