I have the following basic code, which is meant to get number X, add a simple value "+1" just as proof of concept, then save that number back into the database. Also, I require a Django based, jinja template to pull that number and render it onto a website.
My question is, why is the number failing to show up? I only get a blank space where the number is supposed to be rendered and if I remove the [:1] filter, the div is generated 3x; this tells me that the issue might be somewhere in that line but I am at a loss. The code is:
/views.py:
from django.shortcuts import render
from django.http import HttpResponse
from django.template import Context, loader
from home.models import DeathNum
def index(request):
counter = DeathNum.objects.get(pk=1)
fj = counter.deaths
t = loader.get_template('home/basenum.html')
c = {'number_post': str(fj)[:1]}
return HttpResponse(t.render(c))
def increment(request):
counter1 = DeathNum.objects.get(pk=1)
counter1.deaths += 1
counter1.save()
t = loader.get_template('home/basenum.html')
c = {'number_post': str(counter1)[:1]}
return HttpResponse(t.render(c))
/models.py:
from django.db import models
class DeathNum(models.Model):
deaths = models.IntegerField()
def __str__(self):
return "{0}/{1}\n".format(self.id, self.deaths)
/basenum.html:
{% extends "home/index.html" %}
{% block content %}
<br />
<div class="banner">
<div class="bannerNum">
<p div class="numberOf">
Number of deaths in Blank since 1999:
</p>
{% for post in number_post %}
<br /><br /><br />
<p class="death1">
{{ post.deaths }}
</p>
{% endfor %}
</div>
</div>
{% endblock %}