0π
In order to properly send the e-mail by clicking on a link in the template, you should do the following:
-
Create a method in your view.
In this method you may need to do something to obtain the user ID as an input (from the HTTP request, probably) and get the referral code from the database; then send the e-mail with the code provided by wavemode. This would mean something similar to:
def send_referral_code(request):
user = request.user
# Do some work to obtain the referral code from the request.user object
# Do some work to obtain the e-mail from the request.user object
from django.core.mail import send_mail
send_mail('Subject here', 'Message with referral code', 'from@example.com' ['to@example.com'], fail_silently=False) -
Link this method from the urls file in your app.
The following is untested, but should give you an idea:
url(r'^send_referral_code/$', 'user.views.send_referral_code', name='send_referral_code')
. This means that the methodsend_referral_code
can be found in theviews
module, inside theuser
app. -
Refer the previous url from your template.
This is done with a simple
{% url send_referral_code %}
, this time inside thehref
of your link.
Note that this assumes that no input parameter is passed. If you needed some, be sure to add it to the view and urls and finally pass it as the argument in the template, right after the method name (between the %
βs)
There are several basic tutorials, e.g.: https://docs.djangoproject.com/en/dev/intro/tutorial03/#write-your-first-view. You may find examples of urlβs defined with parameters in there.
2π
You can use the Django function send_mail
:
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from@example.com',
['to@example.com'], fail_silently=False)
- [Answered ]-Prevent adding duplicate data from excel forms imported into db
- [Answered ]-How to set default styles for Markdown in email
- [Answered ]-Django PostgreSQL IntegerRangeField and update_or_create