Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Store Leading Zero's in Django

Tags:

python

django

I am currently making a view counter for my website, I am going to have it support a count from anywhere between 0 to 9999. I also want to the view count to always display leading zeros, so if there have been 8 views it would be displayed as 0008. I am having the problem where my model is automatically taking away any leading zero's and I cannot figure out how to remove this. If anyone could help me that would be great, also if the number was stored as 0008 would my +1 method in my view still work? Cheers!

View -

def listing(request, pk):

    job_listing = JobListing.objects.get(pk=pk)

    def view_counter():
        view_count = job_listing.listing_view_counter
        job_listing.listing_view_counter = view_count + 1
        job_listing.save()

    view_counter()

    context_dict = {'joblistings': job_listing}

    return render(request, 'listing.html', context_dict)

Model -

class JobListing(models.Model):

    region_choice = (
        ('Auckland', 'Auckland'),
        ('Wellington', 'Wellington'),
        ('Christchurch', 'Christchurch')
    )
    industry_choice = (
        ('Accounting', 'Accounting'),
        ('Agriculture, fishing & forestry', 'Agriculture, fishing & forestry'),
        ('Automotive', 'Automotive'),
        ('Banking, finance & insurance', 'Banking, finance & insurance'),
        ('Construction & Architecture', 'Construction & Architecture'),
        ('Customer service', 'Customer service'),
    )
    employment_type_choice = (
        ('Full Time', 'Full Time'),
        ('Part Time', 'Part Time'),
        ('One-off', 'One-off'),
        ('Other', 'Other')
    )

    user = models.CharField(max_length=50)
    job_title = models.CharField(max_length=30)
    pay_rate = models.DecimalField(max_digits=10, decimal_places=2)
    employment_type = models.CharField(max_length=10, choices=employment_type_choice)
    job_description = models.CharField(max_length=2000)
    business_address_region = models.CharField(max_length=50, choices=region_choice)
    business_address_suburb = models.CharField(max_length=50)
    business_industry = models.CharField(max_length=50, choices=industry_choice)
    email = models.EmailField(max_length=50, blank=True, null="True")
    telephone = models.IntegerField(blank=True, null='True')
    listing_view_counter = models.IntegerField(default=0)
    active_listing = models.BooleanField(default=True)

    class Meta:
        verbose_name = 'Job Listing'

    def clean(self):
        if not (self.email or self.telep

hone):
            raise ValidationError("You must specify either email or telephone")
        if not self.email:
            self.email = "Not Provided"

def __unicode__(self):
    return "%s" % self.job_title
like image 215
eZ_Harry Avatar asked Dec 05 '22 01:12

eZ_Harry


2 Answers

You can use the stringformat template filter to apply padding to the number inside the template.

For example:

{{ job_listing.listing_view_counter|stringformat:"04d" }}
like image 58
calebbrown Avatar answered Dec 23 '22 03:12

calebbrown


You're making it too hard on yourself. Store the data in your code as it is (as a number), and add the 0 padding in your template.

https://www.djangosnippets.org/snippets/543/

for some code examples to do this.

like image 29
Paul Becotte Avatar answered Dec 23 '22 04:12

Paul Becotte