How do I use mathematical calculations on my forloop Article/Publication list?
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 }}</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+unsubscribe@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/eaa87962-c182-4261-8668-69285dde8de3%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home