Thursday, January 29, 2015

Re: sending email

Couple things. 

I think the return statement for thankyou.html needs to get kicked in a tab, otherwise the form will show successful every time the form is submitted, even if it had errors.If there are errors, it will also skip your email code, but still show as successful.

This line is very confusing:

to_emails = [u.email for u in User.objects.filter(id__in=[a.'id'])]

I don't even think a.'id' is valid syntax. More to the point, your variable 'a' is populated a few lines earlier using a get() queryset, which means it can only contain one item. That would render the list comprehension statement (bad syntax aside) completely useless, since you would only ever have a single element. That also makes the id__in filter unnecessary as well. 

Which leads to another question, are you expecting your User objects to contain the same ID as your newleave objects (for example, User.id == newleave.id). That is indicated by the User.objects query you are making, and I believe you may be incorrect there as well.

Without having seen the model, I'm guessing you want the list of administrative users that are tied to a particular leave, your query line would probably look something like this:

to_emails = [*a.admins.all()]

Also, in your send_mail call, you are encapsulating to_emails inside of another list, like this send_mail(..., [to_emails]). Something tells me you probably don't need to do that, since to_emails is already a list, but I could be wrong there as well.

-James






On Jan 27, 2015 8:55 PM, "sum abiut" <suabiut@gmail.com> wrote:
Can someone please guide me here. i am kinda lost, the other part is ok just the email part i am really lost, please help

here is my view.py the email part is not working

def coporateservices_authorized_Leave(request, id):
    if request.method == 'POST':
        a=newleave.objects.get(id=id)
        form = coporateservices_authoriseleave(request.POST, instance=a)
        if form.is_valid():
           form.save()
           to_emails = [u.email for u in User.objects.filter(id__in=[a.'id'])]
           send_mail("RBV Leave Application Email Testing", "Your Leave have been approved",
          "RBV eLeave <Rbv_eLeave_System>", [to_emails])
        return render_to_response('thankyou.html')
    else:
        a=newleave.objects.get(id=id)
        form =  coporateservices_authoriseleave(instance=a)
    return render_to_response('coporate_services_leave_approvial.html', {'form': form}, context_instance=RequestContext(request))

cheers,





On Wed, Jan 28, 2015 at 7:18 AM, Collin Anderson <cmawebsite@gmail.com> wrote:
Hi,

You should be able to figure out which box is checked from form.cleaned_data.

Collin

On Sunday, January 25, 2015 at 11:53:12 PM UTC-5, suabiut wrote:
Hi Collin,
Thank you very much for your help. its works perfectly.
The next thing i want to do is to send an email to a specific user that i select on a table. when an authorized user select a user to approve his or her leave i want an email to be send to the user. so basically when i click on the checkbox on the Test User form the table below. i should get a form to approve the leave as below. now when i click on authorized Leave button an email should be send to the test user. please point me to right direction.
 Inline image 1

Inline image 2

Cheers,

On Fri, Jan 23, 2015 at 1:18 PM, Collin Anderson <cmawe...@gmail.com> wrote:
Hi,

Just query the users you want.

to_emails = [u.email for u in User.objects.filter(username__in=['person1', 'person2', 'person3'])]
send_mail('Subject here', 'Here is the message.', from_email_address, to_emails)

Collin

On Wednesday, January 21, 2015 at 11:29:51 PM UTC-5, suabiut wrote:
Thanks,
I am trying to send email to the users that their email address is stored in the database on the auth_user table. can someone please point me to the right direction.

Cheers,


On Thu, Jan 22, 2015 at 3:03 PM, Collin Anderson <cmawe...@gmail.com> wrote:
Hi,

Yes, this is possible. Something like:

from django.core.mail import send_mail

def authorized_leave(request):
    form
= MyForm()
   
if request.method == 'POST':
        form
= MyForm(request.POST)
       
if form.is_valid():
            obj
= form.save()

            send_mail
('Subject here', 'Here is the message.', 'fr...@example.com', ['t...@example.com'])
           
return redirect('/done/')
   
return render(request, 'template.html', {form: 'form'})

Collin


On Monday, January 19, 2015 at 10:02:45 PM UTC-5, suabiut wrote:
Hi,
I am trying to send an email to several users once a form is submit. I have done the view.py to update the database once the form is submitted and is working fine. what i want to accomplish when the form is submitted is to update the database and at the same time send out email when a user click on the Authorized leave button.

here is the step i want to do:

 Inline image 1

1) user click on the check box and this form below appears


Inline image 2
 2) the user then fill up the form and click on authorized leave button. the form will stored the information into the database. which i have already done it. what i wanted to do next is to also send a emails when the authorized leave button is click. is it possible to send emails and also save data to database when the authorized leave button is click. i want to send email to the Test User which have his email address store in the database.

template.html
<form action ="" method="post">{%csrf_token%}
<table>
{{form.as_table}}
</table>
<br>
<input type="submit" name="submit" value="Authorized Leave" > 


</form>

please point me to the right direction.

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/3df19a0c-2afc-4e17-b527-83d38a639611%40googlegroups.com.
For more options, visit 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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/619e2173-3ac1-4e98-9b91-5c49966ab968%40googlegroups.com.

For more options, visit 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.
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/7d370efe-301c-4807-a3f3-adf9e980d100%40googlegroups.com.

For more options, visit 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.
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/CAPCf-y78%2BJiYLd53Pn9b3x%2B9vVhmVR-RLRCp1eRdLc_c6ofaKg%40mail.gmail.com.
For more options, visit 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.
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/CA%2Be%2BciWYuYKDqQvu3rK0U8k8Gvps-S_WtJb13NOuhaLUZ0vUqw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment