Python Forum
Django- Remove leading zeros in values from database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django- Remove leading zeros in values from database
#1
Hello,

This is my first post and I'm relatively new to python and django so bear with me.

I'm displaying data from my MySQL database. I would like to remove the leading zeros in a certain fields(the obp and slg fields), but only when the value to the left of the decimal point is a 0. If the digit(s) to the left of the decimal point is not 0, then I obviously want that displayed.

For instance,

I'm displaying 0.750
I would like it displayed as .750

I'm displaying 1.300
I want it to remain being displayed as 1.300

I suspect this can be done in the models or in the views. How would I do either one? Any help on this would be greatly appreciated! Thank You.

models.py
class BattingRegStd(models.Model):
    id = models.IntegerField(db_column='ID', primary_key=True)  # Field name made lowercase.
    hr = models.IntegerField(db_column='HR', blank=True, null=True)  # Field name made lowercase.
    obp = models.FloatField(db_column='OBP', blank=True, null=True)  # Field name made lowercase.
    slg = models.FloatField(db_column='SLG', blank=True, null=True)  # Field name made lowercase.
views.py
from django.shortcuts import render
from .models import BattingRegStd
from .models import FieldingRegStd


# Create your views here.
def batting_reg_std_2018(request):
    battingregstd2018 = BattingRegStd.objects.filter(year=2018)
    return render(request, 'playerstats/battingReg2018.html', {'battingregstd2018':battingregstd2018})
HTML[battingReg2018.html]

{% extends "base.html" %}
{% block contents %}

<div align="center">
<table class="equalDivide">
<thead>
<tr>
<th>HR</th>
<th>OBP</th>
<th>SLG</th>

</tr>
</thead>
<tbody>
{% for index in battingregstd2018%}
<td>{{ index.hr}}</td>
<td>{{ index.obp|default_if_none:"—"|floatformat:3 }}</td>
<td>{{ index.slg|default_if_none:"—"|floatformat:3 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<br>
{% endblock %}
Reply
#2
Probably the easiest way would be with a custom template filter: https://docs.djangoproject.com/en/2.1/ho...late-tags/

It'd probably end up looking something like this:
>>> def no_zero(value):
...   if "." not in value:
...     return value
...   left, right = value.split(".")
...   if "0" == left:
...     return "." + right
...   return value
...
>>> no_zero("12.34")
'12.34'
>>> no_zero("0.34")
'.34'
>>> no_zero("34")
'34'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Django: View is unable to find attributes of database model pythonpaul32 0 484 Dec-07-2023, 06:38 PM
Last Post: pythonpaul32
  Save JSON data to sqlite database on Django Quin 0 2,804 Mar-26-2022, 06:22 PM
Last Post: Quin
  Send email to gmail after user fill up contact form and getting django database updat Man_from_India 0 2,071 Jan-22-2020, 03:59 PM
Last Post: Man_from_India
  Extract tag body without leading/trailing spaces Pavel_47 3 2,188 Nov-26-2019, 08:29 AM
Last Post: Pavel_47
  Posting html values to views/models in Django Malt 1 2,127 Sep-04-2019, 01:44 PM
Last Post: fishhook
  how retrieve database save multiple data in web Django 2.1 taomihiranga 0 2,764 Jul-30-2019, 04:58 PM
Last Post: taomihiranga
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,246 Jun-30-2019, 12:21 AM
Last Post: scidam
  Django- which database i should use? with best speed and performance!! ma_norouzifar 4 4,777 Dec-10-2018, 09:47 AM
Last Post: ma_norouzifar
  How to save uploaded image url in database from Django? PrateekG 14 14,704 Jul-04-2018, 05:18 PM
Last Post: PrateekG
  Need your help to fix my database relationship in Django model PrateekG 0 2,631 Jul-02-2018, 11:08 AM
Last Post: PrateekG

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020