0

I'm trying to add download link inside my email in order to download pdf files stored in media directory. I dont overcome to create this url link in my email content.

This is my views.py with post function :

def post(self, request, *args, **kwargs):
    form = self.form_class()
    document_choice = request.POST.getlist('DocumentChoice')

    if request.method == 'POST':
        form = self.form_class(request.POST)

        for checkbox in document_choice:
            document_edqm_id = Document.objects.get(id=checkbox).edqm_id
            email = request.POST['email']

            plain = email + document_edqm_id + str(datetime.now())
            token = hashlib.sha1(plain.encode('utf-8')).hexdigest()

            Download.objects.create(email=email, pub_id=checkbox, token=token)

            document_link = Document.objects.get(id=checkbox).upload
            print(document_link)

            context = {'document_link': document_link}

            if form.is_valid():
                message = get_template('app/message.txt').render(context)
                html_message = get_template('app/message.html').render(context)
                subject = 'yoto'

                mail = EmailMultiAlternatives(subject, message, 'app@tec.eu', [email])
                mail.attach_alternative(html_message, "text/html")
                mail.send(fail_silently=False)
                print('Email envoyé à ' + email)
            else:
                print('form invalid')
    return HttpResponseRedirect(self.get_success_url())

And my message.txt and message.html file :

#message.txt

== This E-mail is automatically generated, please do not reply to it ==


    {{ document_link }}

You can use this link as much as you like. It will expire in 0 days.

and

#message.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>

<p>== This E-mail is automatically generated, please do not reply to it ==</p>

<br>


<a href="http://localhost:8000/media/{{ document_link}}">Download</a>

<br>

<p>You can use this link as much as you like. It will expire in 0 days.</p>

<br>

</body>
</html>

Result :

I overcome to display file into a new tab, but I want to download this one

Question :

How I can add download file link and not display file in a new tab as up to now ?

Thank you

Essex
  • 6,042
  • 11
  • 67
  • 139
  • 1
    If you have another question, please post it as another question, not as an edit at the bottom of this question. – bruno desthuilliers Sep 10 '18 at 12:56
  • Ok I will do that thank you ;) – Essex Sep 10 '18 at 13:04
  • Possible duplicate of [Force a browser to save file as after clicking link](https://stackoverflow.com/questions/11353425/force-a-browser-to-save-file-as-after-clicking-link) – Aurélien Sep 10 '18 at 14:59
  • @Aurélien It's maybe not a duplicate because add `download` as HTML5 attribut doesn't let to download my file. It displays a new tab with pdf view. – Essex Sep 10 '18 at 15:05

0 Answers0