django how to display number of items











up vote
0
down vote

favorite












I was wondering how I do count the number of poll in my question model using set_count
and I also want to display it in a template please show me using the code thanks



class Question(models.Model):

question_text = models.CharField(max_length=10)

pub_date = models.DateTimeField('date published')


def __str__(self):

return self.question_text

def was_published_recently(self):

return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


class Choice(models.Model):

question = models.ForeignKey(Question, on_delete=models.CASCADE)

title = models.CharField(max_length=10)

site_url = models.URLField()

website_type = models.CharField(max_length=100)

budget = models.CharField(max_length=100)

duration = models.CharField(max_length=100)

description = models.TextField()


I want the show the total of question










share|improve this question




















  • 1




    Please share the models thanks.
    – Willem Van Onsem
    Nov 10 at 22:02















up vote
0
down vote

favorite












I was wondering how I do count the number of poll in my question model using set_count
and I also want to display it in a template please show me using the code thanks



class Question(models.Model):

question_text = models.CharField(max_length=10)

pub_date = models.DateTimeField('date published')


def __str__(self):

return self.question_text

def was_published_recently(self):

return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


class Choice(models.Model):

question = models.ForeignKey(Question, on_delete=models.CASCADE)

title = models.CharField(max_length=10)

site_url = models.URLField()

website_type = models.CharField(max_length=100)

budget = models.CharField(max_length=100)

duration = models.CharField(max_length=100)

description = models.TextField()


I want the show the total of question










share|improve this question




















  • 1




    Please share the models thanks.
    – Willem Van Onsem
    Nov 10 at 22:02













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I was wondering how I do count the number of poll in my question model using set_count
and I also want to display it in a template please show me using the code thanks



class Question(models.Model):

question_text = models.CharField(max_length=10)

pub_date = models.DateTimeField('date published')


def __str__(self):

return self.question_text

def was_published_recently(self):

return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


class Choice(models.Model):

question = models.ForeignKey(Question, on_delete=models.CASCADE)

title = models.CharField(max_length=10)

site_url = models.URLField()

website_type = models.CharField(max_length=100)

budget = models.CharField(max_length=100)

duration = models.CharField(max_length=100)

description = models.TextField()


I want the show the total of question










share|improve this question















I was wondering how I do count the number of poll in my question model using set_count
and I also want to display it in a template please show me using the code thanks



class Question(models.Model):

question_text = models.CharField(max_length=10)

pub_date = models.DateTimeField('date published')


def __str__(self):

return self.question_text

def was_published_recently(self):

return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


class Choice(models.Model):

question = models.ForeignKey(Question, on_delete=models.CASCADE)

title = models.CharField(max_length=10)

site_url = models.URLField()

website_type = models.CharField(max_length=100)

budget = models.CharField(max_length=100)

duration = models.CharField(max_length=100)

description = models.TextField()


I want the show the total of question







django






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 7:33









chris

825714




825714










asked Nov 10 at 21:59









Shadrack Shadrack

62




62








  • 1




    Please share the models thanks.
    – Willem Van Onsem
    Nov 10 at 22:02














  • 1




    Please share the models thanks.
    – Willem Van Onsem
    Nov 10 at 22:02








1




1




Please share the models thanks.
– Willem Van Onsem
Nov 10 at 22:02




Please share the models thanks.
– Willem Van Onsem
Nov 10 at 22:02












1 Answer
1






active

oldest

votes

















up vote
0
down vote













This is what you write in your views.py:



First, to count it, you have to import the model itself






If models.py and views.py both belong to same app:



from .models import Question


If they belong to different apps:



from <appname>.models import Question





I'll assume they belong to the same app, in views.py:



from django.shortcuts import render

from .models import Question

def index(request):
number_of_questions = Question.objects.count()
context = {
'number_of_questions':number_of_questions,
}
return render(request, '<appname>/index.hmtl>', context)


On the first line of the function it just counts the number of questions with .count() method which comes with django. On the second line we define context that we want to use in template, in this case 'number_of_questions':number_of_questions, so in the html template to display this number we'll use {{ number_of_question }}, have we defined it like so: 'questions':number_of_questions, then in the template we would use {{ questions }}, the end result will be that it shows number_of_questions.



In index.html (Or whatever you named the template):



<p>number of questions:</p>
<p>{{ number_of_questions }}


If you have trouble understanding anything I suggest you read through these:

Django documentation on templating



Some information about python dictionaries



EDIT:



I also highly recommend reading this:



Django documentation about making queries to the database






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243835%2fdjango-how-to-display-number-of-items%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    This is what you write in your views.py:



    First, to count it, you have to import the model itself






    If models.py and views.py both belong to same app:



    from .models import Question


    If they belong to different apps:



    from <appname>.models import Question





    I'll assume they belong to the same app, in views.py:



    from django.shortcuts import render

    from .models import Question

    def index(request):
    number_of_questions = Question.objects.count()
    context = {
    'number_of_questions':number_of_questions,
    }
    return render(request, '<appname>/index.hmtl>', context)


    On the first line of the function it just counts the number of questions with .count() method which comes with django. On the second line we define context that we want to use in template, in this case 'number_of_questions':number_of_questions, so in the html template to display this number we'll use {{ number_of_question }}, have we defined it like so: 'questions':number_of_questions, then in the template we would use {{ questions }}, the end result will be that it shows number_of_questions.



    In index.html (Or whatever you named the template):



    <p>number of questions:</p>
    <p>{{ number_of_questions }}


    If you have trouble understanding anything I suggest you read through these:

    Django documentation on templating



    Some information about python dictionaries



    EDIT:



    I also highly recommend reading this:



    Django documentation about making queries to the database






    share|improve this answer

























      up vote
      0
      down vote













      This is what you write in your views.py:



      First, to count it, you have to import the model itself






      If models.py and views.py both belong to same app:



      from .models import Question


      If they belong to different apps:



      from <appname>.models import Question





      I'll assume they belong to the same app, in views.py:



      from django.shortcuts import render

      from .models import Question

      def index(request):
      number_of_questions = Question.objects.count()
      context = {
      'number_of_questions':number_of_questions,
      }
      return render(request, '<appname>/index.hmtl>', context)


      On the first line of the function it just counts the number of questions with .count() method which comes with django. On the second line we define context that we want to use in template, in this case 'number_of_questions':number_of_questions, so in the html template to display this number we'll use {{ number_of_question }}, have we defined it like so: 'questions':number_of_questions, then in the template we would use {{ questions }}, the end result will be that it shows number_of_questions.



      In index.html (Or whatever you named the template):



      <p>number of questions:</p>
      <p>{{ number_of_questions }}


      If you have trouble understanding anything I suggest you read through these:

      Django documentation on templating



      Some information about python dictionaries



      EDIT:



      I also highly recommend reading this:



      Django documentation about making queries to the database






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        This is what you write in your views.py:



        First, to count it, you have to import the model itself






        If models.py and views.py both belong to same app:



        from .models import Question


        If they belong to different apps:



        from <appname>.models import Question





        I'll assume they belong to the same app, in views.py:



        from django.shortcuts import render

        from .models import Question

        def index(request):
        number_of_questions = Question.objects.count()
        context = {
        'number_of_questions':number_of_questions,
        }
        return render(request, '<appname>/index.hmtl>', context)


        On the first line of the function it just counts the number of questions with .count() method which comes with django. On the second line we define context that we want to use in template, in this case 'number_of_questions':number_of_questions, so in the html template to display this number we'll use {{ number_of_question }}, have we defined it like so: 'questions':number_of_questions, then in the template we would use {{ questions }}, the end result will be that it shows number_of_questions.



        In index.html (Or whatever you named the template):



        <p>number of questions:</p>
        <p>{{ number_of_questions }}


        If you have trouble understanding anything I suggest you read through these:

        Django documentation on templating



        Some information about python dictionaries



        EDIT:



        I also highly recommend reading this:



        Django documentation about making queries to the database






        share|improve this answer












        This is what you write in your views.py:



        First, to count it, you have to import the model itself






        If models.py and views.py both belong to same app:



        from .models import Question


        If they belong to different apps:



        from <appname>.models import Question





        I'll assume they belong to the same app, in views.py:



        from django.shortcuts import render

        from .models import Question

        def index(request):
        number_of_questions = Question.objects.count()
        context = {
        'number_of_questions':number_of_questions,
        }
        return render(request, '<appname>/index.hmtl>', context)


        On the first line of the function it just counts the number of questions with .count() method which comes with django. On the second line we define context that we want to use in template, in this case 'number_of_questions':number_of_questions, so in the html template to display this number we'll use {{ number_of_question }}, have we defined it like so: 'questions':number_of_questions, then in the template we would use {{ questions }}, the end result will be that it shows number_of_questions.



        In index.html (Or whatever you named the template):



        <p>number of questions:</p>
        <p>{{ number_of_questions }}


        If you have trouble understanding anything I suggest you read through these:

        Django documentation on templating



        Some information about python dictionaries



        EDIT:



        I also highly recommend reading this:



        Django documentation about making queries to the database







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 8:32









        gigamarr

        237




        237






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243835%2fdjango-how-to-display-number-of-items%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues