Sunday, November 24, 2013

Re: How do I use mathematical calculations on my forloop Article/Publication list?

Thanks airstrike I will check out your suggestions and study it.


Also, is there a reason why you're not using class based views?

Well as a newbie programmer I started out following the Django polls tutorial writing my views using CBVs.  But then when I had trouble doing things with my database and sending my database data through the Views and to the Template pages, since I'm not very familiar with MVC coding.
Explaining my MVC problems was very difficult and it took months before I could get any helpful hints, and when I did get help the CBV related help was too complicated for me to understand and study.

So after 6 months of trying to work with MVC, I signed up for a local Django Meetup.com meeting in real life.  There I grabbed a person who has worked with Django professionally most of their life and showed him my Django polls code project.

Explaining my problems was very difficult because CBVs confuse me so much.  I find it hard to see what is going on under the hood.
Then when I saw how this Django professional had a hard time untangling my View code.  Also he said using CBVs is the preferred way but writing function based Views is simpler to follow.

I then decided to get rid of all my CBVs and write function based Views only.  Suddenly the time it takes for me to explain my MVC problems and get help went down from months to only days. :)

On an academic level I realize using CBVs is the most efficient way but being a total Framework beginner I'm having trouble seeing the real practical advantage of using CBVs compared to simple function based Views.  The function based Views looks more cleaner and lighter to my eyes compared to CBV code and I find it easier to follow what is going on, less magic going on under the hood.






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: 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/b63ec588-f473-4e25-85e2-c7716026607c%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate