0

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 %}

1 Answer 1

2

Answer to your question.

number_post is string. Why for loop to display the str ? Please remove the for loop from your template and just add this...

{{ number_post }}

Or

Another solution to your question

I believe you want to show all the deaths. Please consider below approach?

death_nums = DeathNum.objects.all()
return render(request, 'deaths.html', {'death_nums': death_nums})

And in HTML

{% for death_num in death_nums %}
    {{ death_num.deaths }}
{% endfor %}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm sorry, I don't really understand. You asked me why I'm using a for loop and then in the HTML you introduce another for loop? Perhaps I have more than one for loop and I didn't notice. Can you please clarify what you mean?
@Guren Sorry I've edited my answer now. let me know if something not understand
Your answer helped me out a lot! Especially the idea to remove the for_loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.