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'})
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/619e2173-3ac1-4e98-9b91-5c49966ab968%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7d370efe-301c-4807-a3f3-adf9e980d100%40googlegroups.com.
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
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.
'newleave' object has no attribute 'admins'
here is my model.py and view.py
model.py
class newleave(models.Model):
first_name = models.CharField(max_length=45)
last_name =models.CharField(max_length=45)
department=models.CharField(max_length =45)
position=models.CharField(max_length =45)
leave_type =models.CharField(max_length=45)
specify_details=models.TextField(default="")
start_date =models.DateField(null=True)
end_date=models.DateField(null=True)
total_working_days=models.IntegerField(null=True)
department_head_authorization =models.CharField(max_length=45, default ="")
authorized_by=models.CharField(max_length=45, default ="")
remarks=models.TextField()
authorization_date =models.DateField(null=True)
corporate_services_authorization =models.CharField(max_length=45)
authorized_by1=models.CharField(max_length=45)
remarks1=models.TextField(default ="")
authoriztaion1_date =models.DateField(null=True)
total_Leave_Left =models.IntegerField(default=20)
username =models.ForeignKey(User, default =1)
view.py
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 = [a.admins.all()]
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))
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWYuYKDqQvu3rK0U8k8Gvps-S_WtJb13NOuhaLUZ0vUqw%40mail.gmail.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.
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/CA%2BxOsGBHGrjKgDV7ZvS%2Bs1EUm571bpQzdGPEn6MGyRKvUqYh2w%40mail.gmail.com.
Oh, I see what you are trying to do. It looks like you are running needless queries though.
Your newleave model has this:
username =models.ForeignKey(User, default =1)
That's an FK to User, which means this line in your view:
to_emails = [u.email for u in User.objects.filter(username__in=[a.username])]
is only querying for information that you already have, and trying to use list comprehension for a query that should only ever return a single result. The line above should be shortened to the following:
to_emails = [ a.username.email ]
I was previously assuming that you wanted to email the admins of the leave, hence the funny query with a.admins, etc (which was a pure guess since I hadn't seen the model). Now I can see that you want to email the user that is directly attached to the leave. To make it even more slick, in addition to the change above, you can also add a select_related('username') to the original newleave query so that you'll only run a single query, which is much more efficient, like this:
def coporateservices_authorized_Leave(request, id):
if request.method == 'POST':
a=newleave.objects.select_related('username').get(id=id)
form = coporateservices_authoriseleave(request.POST, instance=a)
if form.is_valid():
form.save()
to_emails = [ a.username.email ]
send_mail("RBV Leave Application Email Testing", "Your Leave Application have been approved by"+" "+a.authorized_by1,
"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))
Other musings:
Try to be more consistent with your naming conventions for your classes and function/view names. For example, I would have named your newleave class as NewLeave, using CamelCase (aka CapCase) for all class names. Check out Python's PEP8 for more style guidelines: https://www.python.org/dev/peps/pep-0008/
I would also recommend using string token replacement to fill in strings using data from variables rather than concatenating them using the + notation. For example:
"Your Leave Application have been approved by {}".format(a.authorized_by1)
It is more conventional and will make it easier to start passing in strings for translation later, if needed.
Glad you got it working though.
-James
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPCf-y4aJYmh1zS--w5vdi6dPJpW_acruQY-JfWbtWUwFgOwHg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWMSDpoKgdzfEEnnVA_q-SVkGqUhgobrZYe-8bb%3DmVR5w%40mail.gmail.com.