passing data to template

64 views
Skip to first unread message

sum abiut

unread,
Jun 13, 2017, 12:54:12 AM6/13/17
to django...@googlegroups.com
Hi,
I need some help, i am using Django as my web framework and sqlalchemy to query my database. When i pass data template its not showing on the template. can someone point me to the right direction.

The post date is loaded on the template but the Net Balance is not loaded. Don't know what i am doing wrong here.

below is my view.py and template


view.py

def SUMCORP(request):
    engine=create_engine('mssql+pymssql://username:pass@servername /RBVData')
    connection=engine.connect()
    metadata=MetaData()
    fund=Table('gltrxdet',metadata,autoload=True,autoload_with=engine)
    stmt=select([fund.columns.date_posted,func.sum(fund.columns.nat_balance)])
    stmt = stmt.where(fund.columns.account_code=='002CORP')
    stmt=stmt.group_by(fund.columns.date_posted)
    results=connection.execute(stmt).fetchall()
    return render_to_response('fundmanager_sum.html',locals())


template.html

<table class="table table-striped">
  <thead>
<tr>
 
  <th>Net Balance</th>
  <th>Post Date</th>
</thead>

</tr>
{%for a in results%}

<tr>

<td>{{a.nat_balance}}</td>
    <td>{{a.date_posted}}</td>

  </tr>

{%endfor%}
</table>

ludovic coues

unread,
Jun 13, 2017, 2:34:46 AM6/13/17
to django...@googlegroups.com
Try `{{ results | pprint }}` in your template.

That will not solve your problems but that will give you a lot more information about what data have been passed to your template.

--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPCf-y7tGoLm2hxPaQpBrrm_j0VJvTF1UvE%3D-vQgbjh%3DzCxEMg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

sum abiut

unread,
Jun 13, 2017, 4:57:30 PM6/13/17
to django...@googlegroups.com
Hi ludovic,
Thanks for your response.
when i did what you say it print out the date and sum in an array. Date still in Julian date but i don't worry about the date now just want to have the sum display nicely in my template

[(733011, 28397.54), (733030, 5136.79), (733044, 35.89), (733048, 0.0), (733049, 0.0), (733077, 6313.83), (733084, 6370.96), (733091, 150.0), (733104, -500.0), (733107, 0.0), (733108, 500.0), (733133, 5967.62), (733154, 7054.0), (733164, 0.0), (733174, 5698.18), (733195, 0.0), (733212, 5469.15), (733224, 0.0), (733255, 4887.2), (733265, -70000.0), (733286, 0.0), (733316, 0.0), (733342, -119.78999999999999), (733349, 0.0), (733377, 0.0), (733409, 0.0), (733422, -241.0), (733426, 241.0), (733440, 0.0), (733468, 0.0), (733498, 0.0), (733531, 0.0), (733559, 0.0), (733582, 104.59), (733589, 0.0), (733622, 0.0), (733651, 0.0), (733671, 3315.0), (733681, 0.0), (733713, 0.0), (733742, 0.0), (733772, -83.89), (733776, 0.0), (733777, 0.0), (733804, 0.0), (733832, 0.0), (733863, 0.0), (733895, 0.0), (733924, 0.0), (733948, 4.33), (733954, 0.0), (733986, 0.0), (734016, 0.0), (734046, 0.0), (734053, 0.0), (734077, 0.0), (734107, 0.0), (734137, -35.65), (734169, 0.0), (734197, 0.0), (734228, 0.0), (734260, 0.0), (734289, 0.0), (734319, 0.0), (734350, 0.0), (734381, 0.0), (734413, 0.0), (734442, 0.0), (734472, 0.0), (734501, -31.380000000000003), (734505, 0.0), (734534, 0.0), (734563, 0.0), (734595, 0.0), (734625, 0.0), (734655, 0.0), (734686, 0.0), (734716, 0.0), (734749, 0.0), (734777, 0.0), (734808, 0.0), (734840, 0.0), (734868, 0.0), (734900, 0.0), (734931, 0.0), (734960, 0.0), (734990, 3455.0), (735022, 1030.0), (735050, -2099.0), (735081, 460.0), (735113, -102.0), (735142, -980.0), (735173, 1538.9), (735204, 920.0), (735231, -500.0), (735233, 0.0), (735267, -1000.0), (735295, -1150.0), (735325, -250.0), (735355, 0.0), (735386, -560.69), (735415, 570.0), (735446, 0.0), (735477, -300.0), (735507, -205.70999999999998), (735540, 1600.0), (735569, -1040.0), (735598, -1404.31), (735614, 8.62), (735631, 0.0), (735649, 0.0), (735659, 0.0), (735689, 5.0), (735722, 0.0), (735750, 135.0), (735780, 770.0), (735813, 265.0), (735842, -1165.0), (735872, 30.0), (735904, -55.0), (735933, 4.29), (735967, 1105.0), (735968, 0.0), (735990, 4.32), (735995, 0.0), (736024, 0.0), (736055, 0.0), (736087, -1000.0), (736116, 0.0), (736146, 0.0), (736178, 0.0), (736208, 185.0), (736240, -285.68), (736269, 0.0), (736299, 0.0), (736332, 44.32), (736361, 0.0), (736389, 0.0), (736422, 0.0), (736451, -40.0), (736481, 0.0)]


Simon McConnell

unread,
Jun 13, 2017, 6:16:01 PM6/13/17
to Django users
<td>{{a.nat_balance}}</td>

sure it shouldn't be spelt

<td>{{a.net_balance}}</td>


?
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

--
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...@googlegroups.com.

sum abiut

unread,
Jun 13, 2017, 11:16:59 PM6/13/17
to django...@googlegroups.com
Yes,
on the database it spelt nat_balance

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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

ludovic coues

unread,
Jun 14, 2017, 2:30:01 PM6/14/17
to django...@googlegroups.com
<table class="table table-striped">
  <thead>
<tr>
  <th>Net Balance</th>
  <th>Post Date</th>
</thead>
</tr>
{%for a in results%}
<tr>
  <td>{{a.0}}</td>
  <td>{{a.1}}</td>
</tr>
{%endfor%}
</table>

`{{results|pprint}}` is pretty clear. You have a list of tuple. Tuples have no named member so a.nat_balance return a None value.
{{ a.0 }} in your django template is the equivalent of a[0] in python.

If you want dict-like object, your problem is with SQLalchemy, not django


For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Ludovic Coues
+33 6 14 87 43 42

sum abiut

unread,
Jun 14, 2017, 5:47:56 PM6/14/17
to django...@googlegroups.com
Thanks heaps. appreciate your help, pointing me to right direction.



sum abiut

unread,
Jun 14, 2017, 10:08:07 PM6/14/17
to django...@googlegroups.com
I try to perform a division on the tuple but i am getting an error. I have added another column call Gl rate. i try to perform the division lin
<td>{{a.1/a.0}}</td> but im geting an error. not sure what i am doing wrong here.



<table class="table table-striped">

  <thead>
<tr>
  <th>Net Balance</th>
  <th>Post Date</th>
  <th> GL rate</th>

</thead>
</tr>
{%for a in results%}
<tr>
  <td>{{a.0}}</td>
  <td>{{a.1}}</td>
  <td>{{a.1/a.0}}</td>
</tr>
{%endfor%}
</table>

Mike Dewhirst

unread,
Jun 14, 2017, 10:53:14 PM6/14/17
to django users
On 15/06/2017 12:07 PM, sum abiut wrote:
> I try to perform a division on the tuple but i am getting an error. I
> have added another column call Gl rate. i try to perform the division lin
> <td>{{a.1/a.0}}</td>but im geting an error. not sure what i am doing
> wrong here.
>
You are asking the template to perform division. There doesn't seem to
be a "divide" filter.

I don't know what the experts will advise but you might have to write
one yourself.
https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/

However, I believe the accepted wisdom is that computation in the
template is more expensive than in the view. Hence I would likely do it
there in ordinary Python and pass a straight result to the template.

That said, you might also consider
https://pypi.python.org/pypi/django-mathfilters
> <mailto:sua...@gmail.com>>:
>
> Yes,
> on the database it spelt nat_balance
>
>
> On Wed, Jun 14, 2017 at 9:16 AM, Simon McConnell
> <simonmc...@gmail.com
> <https://groups.google.com/group/django-users>.
> <https://groups.google.com/d/msgid/django-users/CAPCf-y7tGoLm2hxPaQpBrrm_j0VJvTF1UvE%3D-vQgbjh%3DzCxEMg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit
> https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> 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...@googlegroups.com.
> To post to this group, send email to
> django...@googlegroups.com.
> Visit this group at
> https://groups.google.com/group/django-users
> <https://groups.google.com/group/django-users>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAEuG%2BTYqadry9dT8O2R0XQcivXae2OGWYaA%2BoefTmO4wL22DCQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAEuG%2BTYqadry9dT8O2R0XQcivXae2OGWYaA%2BoefTmO4wL22DCQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit
> https://groups.google.com/d/optou
> <https://groups.google.com/d/optout>
>
>
> --
> 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...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to
> django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/group/django-users>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/81c3ba9f-c47d-4e85-8987-0c306f26b467%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/81c3ba9f-c47d-4e85-8987-0c306f26b467%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
>
> For more options, visit
> https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
>
>
>
>
> --
> 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...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to
> django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/group/django-users>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAPCf-y7cvSm0YrGh24Fiz_N4xuCHijDxpP0r3P%3Db37XqHCuQYQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAPCf-y7cvSm0YrGh24Fiz_N4xuCHijDxpP0r3P%3Db37XqHCuQYQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
>
>
> --
>
> Cordialement, Ludovic Coues
> +33 6 14 87 43 42 <tel:+33%206%2014%2087%2043%2042>
> --
> 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...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to
> django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/group/django-users>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAEuG%2BTYbimh5EKdsS9%3DDX291hLkz-hcZ27SF%3DHQstm3Uc8e1YA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAEuG%2BTYbimh5EKdsS9%3DDX291hLkz-hcZ27SF%3DHQstm3Uc8e1YA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
>
>
>
> --
> 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...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAPCf-y6jwX0dh1vHudHhPAhnf-amUmR%3D5uh%2Bkxdsc%2BbLZrQwCg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAPCf-y6jwX0dh1vHudHhPAhnf-amUmR%3D5uh%2Bkxdsc%2BbLZrQwCg%40mail.gmail.com?utm_medium=email&utm_source=footer>.

sum abiut

unread,
Jun 15, 2017, 12:42:40 AM6/15/17
to django...@googlegroups.com
Hi Mike,
Thank heaps for direction. Appreciate your help.

Cheers,

                django-users+unsubscribe@googlegroups.com
                <mailto:django-users+unsubscrib...@googlegroups.com>.

                To post to this group, send email to
                django...@googlegroups.com
                <mailto:django-users@googlegroups.com>.

                Visit this group at
                https://groups.google.com/group/django-users
                <https://groups.google.com/group/django-users>.
                To view this discussion on the web visit
                https://groups.google.com/d/msgid/django-users/81c3ba9f-c47d-4e85-8987-0c306f26b467%40googlegroups.com
                <https://groups.google.com/d/msgid/django-users/81c3ba9f-c47d-4e85-8987-0c306f26b467%40googlegroups.com?utm_medium=email&utm_source=footer>.


                For more options, visit
                https://groups.google.com/d/optout
                <https://groups.google.com/d/optout>.






            --             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

            To post to this group, send email to
            django...@googlegroups.com
            <mailto:django-users@googlegroups.com>.

            Visit this group at
            https://groups.google.com/group/django-users
            <https://groups.google.com/group/django-users>.
            To view this discussion on the web visit
            https://groups.google.com/d/msgid/django-users/CAPCf-y7cvSm0YrGh24Fiz_N4xuCHijDxpP0r3P%3Db37XqHCuQYQ%40mail.gmail.com
            <https://groups.google.com/d/msgid/django-users/CAPCf-y7cvSm0YrGh24Fiz_N4xuCHijDxpP0r3P%3Db37XqHCuQYQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.

            For more options, visit https://groups.google.com/d/optout
            <https://groups.google.com/d/optout>.




        --
        Cordialement, Ludovic Coues
        +33 6 14 87 43 42 <tel:+33%206%2014%2087%2043%2042>
        --         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
        <mailto:django-users+unsubscrib...@googlegroups.com>.

        To post to this group, send email to
        django...@googlegroups.com
        <mailto:django-users@googlegroups.com>.

        Visit this group at
        https://groups.google.com/group/django-users
        <https://groups.google.com/group/django-users>.
        To view this discussion on the web visit
        https://groups.google.com/d/msgid/django-users/CAEuG%2BTYbimh5EKdsS9%3DDX291hLkz-hcZ27SF%3DHQstm3Uc8e1YA%40mail.gmail.com
        <https://groups.google.com/d/msgid/django-users/CAEuG%2BTYbimh5EKdsS9%3DDX291hLkz-hcZ27SF%3DHQstm3Uc8e1YA%40mail.gmail.com?utm_medium=email&utm_source=footer>.


        For more options, visit https://groups.google.com/d/optout
        <https://groups.google.com/d/optout>.





--
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 <mailto:django-users+unsubscrib...@googlegroups.com>.
To post to this group, send email to django...@googlegroups.com <mailto:django-users@googlegroups.com>.
--
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...@googlegroups.com.

sum abiut

unread,
Jun 15, 2017, 10:13:04 PM6/15/17
to django...@googlegroups.com
Is it possible to pass data from two different django functions to a one template in a single for loop?

Cheers,

Mike Dewhirst

unread,
Jun 16, 2017, 1:10:19 AM6/16/17
to Django users
On 16/06/2017 12:12 PM, sum abiut wrote:
> Is it possible to pass data from two different django functions to a
> one template in a single for loop?

Yes. The general way to do that (and pretty much all Django things) is
to write a "view" which can contain as many functions as desired and
fetch as much data from the database as desired.

The view software then creates one or more named object(s) or perhaps
assembles a dictionary or a list of tuples or a tuple of tuples and
renders that data via a named template.

... here go all your functions to create myObject and myOtherThing ...
... They can both be passed to the template as follows ...

return render(
request=request,
template_name='mytemplate.html',
context={
'thing'=myObject,
'otherthing'=myOtherThing,
}
)

The template expects the named object or objects or dictionaries etc and
displays the data via template variables for example

{% if otherthing %}
<p>Other thing exists: {{ otherthing.percentage }} </p>
{% endif %}
{% if thing.thing_list %}
{% for item in thing.thing_list %}
{{ item.name }} {{ item.size }} {{ item.modified_date }}
{% endfor %}
{% endif %}

hth

>
> Cheers,
> --
> 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...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAPCf-y4nSH-WQUcR4sCzy6OSg9q_dVTpEbf2%2BrDmUF-fmgRrJA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAPCf-y4nSH-WQUcR4sCzy6OSg9q_dVTpEbf2%2BrDmUF-fmgRrJA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages