I worked some more on James' hint:  articles[0].num_publications() 

And I followed some tutorial exercises on making Template filters.  After 
seeing DJ-Tom's hint.

And this is as far as I got from trying things...
I'm trying to send two lists to my template so I can do some parallel 
iteration in my forloop.
But then I realized that *type(articles)* is a Queryset and not a list so I 
can't use zip() like in the examples I'm reading.

So how can I integrate my list
    *publicists_coverage* = [] # List 3
into this forloop template code?
    {% for row in *articles* %}


#_______________________________________________________________________________

def index(request):
    publications = Publication.objects.all()

    # sum_publications = Publication.objects.filter(article__pk=1).count()

    # List 1
    *articles* = 
Article.objects.all().annotate(num_publications=Count('publications'))
    #>>> type(articles)
    #<class 'django.db.models.query.QuerySet'>

    # 3
    publication_per_article = [] # List 2
    for i in range(articles.count()):
        x = articles[i].num_publications
        publication_per_article.append(x)

    # 5
    total_publicists = Publication.objects.all().count()

    # Calculate things!
    *publicists_coverage* = [] # List 3
    for row in publication_per_article:
        y = float(row)/total_publicists
        publicists_coverage.append(y)

    ziplist = zip(articles, publicists_coverage)
    context = {'publications':publications, 'articles':articles,
               'total_publicists':total_publicists, 'ziplist':ziplist}
#    context = {'publications':publications,
#               'total_publicists':total_publicists, 'ziplist':ziplist}
    return render(request, 'magazine/index.html', context)
#_______________________________________________________________________________

{% if *articles* and ziplist %}
    <table border="1">
    <tr>
        <th>Article id</th>
        <th>Article</th>
        <th>Publication</th>
        <th>Publicists coverage (%)</th>
    </tr>
    {% for row in *articles* %}
    <tr>
        <td>{{ row.id }}</td>
        <td>{{ row.headline }}</td>
        <td>{{ row.num_publications }}</td>
        <td>({{ row.num_publications }} / {{ total_publicists }}) = {{ 
publicists_coverage }}x %</td>
    </tr>
    {% endfor %}
    </table>
{% else %}
    <p>No Articles or Ziplist are available.</p>
{% endif %}








On Wednesday, November 20, 2013 10:29:24 PM UTC+1, Pepsodent Cola wrote:
>
> 1.)
> How do I use mathematical calculations on my forloop Article/Publication 
> list?
>
> * I have attached a screenshot with the table field that I'm working on 
> *"Publicists 
> coverage (%)"*.
> * Admin user/pass = joe/joe
> * I have also attached my project files so that you can see things for 
> yourselves.
>
>
> 2.)
> Is the solution I'm working with in "Index View page" the correct practice 
> to do my mathematical calculations?
>
>
> #_______________________________________________________________________________
>
> def index(request):
>     publications = Publication.objects.all()
>
>
>     # sum_publications = Publication.objects.filter(article__pk=1).count()
>     articles = 
> Article.objects.all().annotate(num_publications=Count('publications'))
> *    #articles_int = int(articles)*
>
> *    sum_publicists = Publication.objects.all().count()*
>
> *    #publicists_coverage = articles/sum_publicists*
>
>
> #    context = {'publications':publications, 
> 'sum_publicists':sum_publicists,
> #               'publicists_coverage':publicists_coverage, 
> 'articles':articles}
>     context = {'publications':publications, 
> 'sum_publicists':sum_publicists,
>                'articles':articles}
>     return render(request, 'magazine/index.html', context)
>
> #_______________________________________________________________________________
>
> <article>
>     <h1>How many Publications was each Article in?</h1>
>
> {% if articles %}
>     <table border="1">
>     <tr>
>         <th>Article id</th>
>         <th>Article</th>
>         <th>Publication</th>
> *        <th>Publicists coverage (%)</th>*
>     </tr>
>     {% for row in articles %}
>     <tr>
>         <td>{{ 
> row.id<http://www.google.com/url?q=http%3A%2F%2Frow.id&sa=D&sntz=1&usg=AFQjCNGG971NtAwWsaCVNskfLhyoY4rmew>}}</td>
>         <td>{{ row.headline }}</td>
>         <td>{{ row.num_publications }}</td>
>         <td>(*{{ row.num_publications }} / {{ sum_publicists }}*) = x 
> %</td>
>     </tr>
>     {% endfor %}
>     </table>
> {% else %}
>     <p>No Articles are available.</p>
> {% endif %}
>
>     <hr>
> </article>
>
>
> #_______________________________________________________________________________
>
>
> 3.)
> When I try to convert articles object into int it doesn't work.  Am I 
> going the wrong direction when thinking about solving things like this?
>
>
> Exception Type: TypeError
> Exception Value: *int() argument must be a string or a number, not 
> 'QuerySet'*
> Exception Location: /home/linux/Django/ManyToMany/magazine/views.py in 
> index, line 17
>
>
> >>> articles = 
> Article.objects.all().annotate(num_publications=Count('publications'))
> >>> type(articles)
> <class 'django.db.models.query.QuerySet'>
> >>> 
> >>> *articles_int = int(articles)*
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
> TypeError: int() argument must be a string or a number, not 'QuerySet'
> >>> 
>
>
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/f961e7ed-99ef-4c3f-93f8-233a60211dd6%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to