Friends this is the continuation from its previous post so i kindly recommend you to read that post i have made this very simple even a complete beginner can learn it and if you are beginner in python then you need to learn python i have made that blog also for that click this link
PAGE REDIRECTIONS
The redirection is necessary when you need your user to go to a different page when the desired direction is on error or due to some other reasons In Django, redirection is accomplished using the 'redirect' method.
The 'redirect' method takes as argument: The URL you want to be redirected to as string A view's name
The myproj/views looks like the following so far −The 'redirect' method takes as argument: The URL you want to be redirected to as string A view's name
from django.shortcuts import render, redirect from django.http import HttpResponse import datetime # Create your views here. def index(request): today = datetime.datetime.now().date() return redirect("https://www.djangoproject.com")def myno(request, myno):
""" A view that display a name based on his ID""" text = "Displaying Number : %s" %myno return redirect(mynumbers, year = "2045", month = "02") def mynumbers(request, year, month): text = "Displaying numbers of : %s/%s"%(year, month) return HttpResponse(text)
Here we have changed our view to redirect our index view to the django official site by adding the complete url of the site in the redirect method in the index view
and the myno view is redirected to the mynumbers view here by giving its name as parameter here
It is also possible to specify whether the 'redirect' is temporary or permanent by adding permanent = True parameter. The user will see no difference, but these are details that search engines take into account when ranking of your website.
It is also possible to specify whether the 'redirect' is temporary or permanent by adding permanent = True parameter. The user will see no difference, but these are details that search engines take into account when ranking of your website.
SENDING E-MAIL
The django use the help of import django.core.mail. as of like in python it is import of smtplib to start sending email we need to edit our settings .py file and set the following options which is
- EMAIL-HOST − smtp server host
- EMAIL_HOST_USER − Login credential for the smtp server.
- EMAIL_HOST_PASSWORD − Password credential for the smtp server.
- EMAIL_PORT − smtp server port.
- EMAIL_USE_TLS or _SSL − True if secure connection
from django.core.mail import send_mail
from django.http import HttpResponse
def sendSimpleEmail(request,emailto):
res = send_mail('Subject here','Here is the message.','from@example.com',['to@example.com'],fail_silently=False,)
return HttpResponse('%s'%res)
from django.http import HttpResponse
def sendSimpleEmail(request,emailto):
res = send_mail('Subject here','Here is the message.','from@example.com',['to@example.com'],fail_silently=False,)
return HttpResponse('%s'%res)
here the send_mail has the things which a email is composed of like the parameters first which is subject of the mail which will be a string then comes the main content or the message which is to be sent of the mail which will be a string then the from address or the mail id of the sender it is also a string and the mail id of the receiver A list of strings, each an email address. Each
member of
The other parameters used in here are
recipient_list
will see the other recipients in the “To:”
field of the email message. then comes the fail_silently which comes Bool, if false send_mail will raise an exception in case of error.The character set of email sent with django.core.mail will be set to the value of your DEFAULT_CHARSET setting.The other parameters used in here are
- auth_user − User login if not set in settings.py.
- auth_password − User password if not set in settings.py.
- connection − E-mail backend.
- html_message − (new in Django 1.7) if present, the e-mail will be multipart/alternative.
The return value will be the number of successfully delivered messages (which can be 0 or 1 since it can only send one message).
Sending Multiple Mails with send_mass_mail()
The send_mass_mail is intended to handle mass mailing were it have an extra parameter different to send_mail is a datatuple for example
send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None, connection=None)
fail_silently ,auth_user,auth_password,has the same function as in the send_mail() and the datatuple is a tuple with each element is in a format
(subject, message, from_email, recipient_list)
The connection in here is a email backend Each element of datatuple results in a separate email message. As in send_mail(), recipients in the same recipient_list will all see the other addresses in the email messages’ “To:” field.
from django.core.mail import send_mass_mail
from django.http import HttpResponse
def sendMassEmail(request,emailto):
msg1 = ('subject 1', 'message 1', 'nads@sani.com', [recipient1])
msg2 = ('subject 2', 'message 2', 'nads@sani.com', [recipient2])
res = send_mass_mail((msg1, msg2), fail_silently = False)
return HttpResponse('%s'%res)
from django.http import HttpResponse
def sendMassEmail(request,emailto):
msg1 = ('subject 1', 'message 1', 'nads@sani.com', [recipient1])
msg2 = ('subject 2', 'message 2', 'nads@sani.com', [recipient2])
res = send_mass_mail((msg1, msg2), fail_silently = False)
return HttpResponse('%s'%res)
Here we have send the messages and the return value will be the number of successfully delivered messages the main difference between send_mail and send_mass_mail is that the send_mail opens a connection to the mail server each time its executed and the send_mass_mail uses a single connection for all of its messages this confirms that the send_mass_mail() is slightly more efficient
Sending mail to admins
Sending mails to admin is done by the shortcut django.core.mail.mail_admins as defined in the ADMINS settings mail_admins prefixes the value of the EMAIL_SUBJECT_PREFIX setting which is[Django] by default the from header of the email will be the value of SERVER_EMAIL setting this method exists for convenience and readability If html_message is provided, the resulting email will be a multipart/alternative email with message as the text/plain content type and html_message as the text/html content type. an example for sending mail to admin is as
from django.core.mail import mail_admins
from django.http import HttpResponse
def sendAdminsEmail(request):
res = mail_admins('my subject', 'site is going down.')
return HttpResponse('%s'%res)
from django.http import HttpResponse
def sendAdminsEmail(request):
res = mail_admins('my subject', 'site is going down.')
return HttpResponse('%s'%res)
It will send the mail to the admin mail id as defined in the ADMIN settings
SENDING MAIL TO MANAGERS
the method of sending mail to managers is also similar to sending mail to admins for example here it is as
from django.core.mail import mail_managers
from django.http import HttpResponse
def sendManagersEmail(request):
res = mail_managers('my subject 2', 'Change date on the site.')
return HttpResponse('%s'%res)
from django.http import HttpResponse
def sendManagersEmail(request):
res = mail_managers('my subject 2', 'Change date on the site.')
return HttpResponse('%s'%res)
as explained earlier it is as similar to sending mail to admins in here it is sending it to managers
that's all for this post friends learn more from the next post.
that's all for this post friends learn more from the next post.
No comments:
Post a Comment