1.)
I managed to make my View calculate my variables but I'm getting the same result "0.6" for each forloop.
What am I doing wrong with my variables calculation?
* I have attached an updated screenshot with the table field that I'm working on "Publicists coverage (%)".
* Admin user/pass = joe/joe
* I have also attached an update of my project files so that you can see things for yourselves.
#_______________________________________________________________________________
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 = articles.count()
sum_publicists = Publication.objects.all().count()
publicists_coverage = float(articles_int)/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)
#_______________________________________________________________________________
<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 }}) = {{ publicists_coverage }}x %</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>No Articles 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 }}</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: TypeErrorException 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/3ad90f7c-3d76-430b-9dff-47d35c4367b6%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:
Post a Comment