forloop

28 views
Skip to first unread message

sum abiut

unread,
Jan 26, 2015, 5:49:27 PM1/26/15
to django...@googlegroups.com
Hi,
i am getting an error as below when iterating throw rows. Basically what i want to do is iterate through number of rows. When on the last row i want to compare a  column then send out an email.

here is the error that i am getting:

global name 'forloop' is not defined


here is my view.py:

def email_authorizer(request):
    new_leave=newleave.objects.all()
    for email in new_leave:
        if forloop.last:
            govoffice=newleave.objects.filter(department="GOV")
            if govoffice.exists():
                to_emails = [u.email for u   in User.objects.filter(username__in['testuser','sabiut'])]
                send_mail("RBV Leave Application Email Testing", "You have received a leave application form. Please login to the leave system to Authorize the leave.",
                "RBV eLeave <Rbv_eLeave_System>", [to_emails])
                return render_to_response('thankyou.html')

Paul Royik

unread,
Jan 26, 2015, 6:28:37 PM1/26/15
to django...@googlegroups.com
Inside view there is no forloop (unless you explicitly define it). forloop is defined only inside for loops in templates

Actually your code is somewhat weird.
You iterate all list until last without doing anything.

It is more efficient to use slicing

email = newleave.objects.all()[-1] # this is already last item
govoffice=newleave.objects.filter(department="GOV")
            if govoffice.exists():
                to_emails = [u.email for u   in User.objects.filter(username__in['testuser','sabiut'])]
                send_mail("RBV Leave Application Email Testing", "You have received a leave application form. Please login to the leave system to Authorize the leave.",
                "RBV eLeave <Rbv_eLeave_System>", [to_emails])
                return render_to_response('thankyou.html') 

Other code is also not very understandable, but I can't help you, since don't understand what should be done.

sum abiut

unread,
Jan 26, 2015, 6:38:48 PM1/26/15
to django...@googlegroups.com
Thank you for your email. I am iterating all the row to find out which row was recently added. basically when a row is added to it become last. so i am trying to comparing a column on the last row. which is the row that was recently add if the condition of the column is exist. that is when i send out email.

hope my explanation is clear.

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.
To post to this group, send email to django...@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/ac2740f3-2e60-44f7-860d-974550476cd4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Stephen J. Butler

unread,
Jan 26, 2015, 6:52:22 PM1/26/15
to django...@googlegroups.com
Do you have a default ordering on the Model object? That is, an
ordering option on newleave.Meta? Because otherwise this query
"newleave.objects.all()" is not at all guaranteed to return rows in
the order they were added.

It may, by coincidence, in your current environment behave that way.
But change databases, or add more rows, or look at it funny, and that
won't be true.
> https://groups.google.com/d/msgid/django-users/CAPCf-y51_QA6iLbJnyvLQSkdL6SCu8x%3DO8P2vjVeWcBsd9rCWQ%40mail.gmail.com.

sum abiut

unread,
Jan 26, 2015, 7:02:19 PM1/26/15
to django...@googlegroups.com
Here is how it looks like, when a new row is added it display last.
Inline image 1

James Schneider

unread,
Jan 27, 2015, 2:04:03 AM1/27/15
to django...@googlegroups.com
Instead of looping through the all() set of objects (which works quickly with small sets of objects, imagine >20K objects, that would be noticeably slower, probably seconds or more),  you should just query the object you want directly using something like:

obj = newleave.objects.order_by('-pk')[0]

That would give you the newleave object that has the highest PK, and would presumably be the most recently added entry. No need for a loop, let the DB do the work in this case. 

I'm a bit confused by the logic in the for loop. You re-query newleave.objects.filter(department="GOV"), which would return a bunch of rows no matter what (based on the output you provided), and has no relation based on the object that you asked about initially. I don't think that does what you want, as it will send out an email every time no matter what, not just every time that obj.department == 'GOV' (which is probably the check you want instead). 

-James

sum abiut

unread,
Jan 27, 2015, 5:34:04 PM1/27/15
to django...@googlegroups.com
Hi James,
Thanks very much for shearing your intellect, i really appreciate your help. It works perfectly well. i want the users who receive the email to be able to know who have apply for the leave: where it is highlighted blue i want to put like test user have apply for a leave. could you please advise how to do that.
here is my view.py

def email_authorizer(request):
    email_obj = newleave.objects.order_by('-pk')[0]
    if email_obj.department == 'GOV':
        to_emails = [u.email for u in User.objects.filter(username__in=['sabiut'])]
        send_mail("RBV Leave Application Email Testing", "You have received a leave application form. Please login to the leave system to Authorize the leave. \nPlease ignore this message, we are testing the new eLeave system!",

        "RBV eLeave <Rbv_eLeave_System>", [to_emails])
        return render_to_response('thankyou.html')


Cheers,






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



--

sum abiut

unread,
Jan 27, 2015, 7:41:20 PM1/27/15
to django...@googlegroups.com
Thanks James,
I have figure out how to do that. thanks again for your help.
-
Reply all
Reply to author
Forward
0 new messages