Reputation: 16765
I have a django project. index.html page of it contains a form with submit button. I want to include django-template in this index.html page to send email with data from this form. Is it possible to do without any python-code? Just like:
{% send_mail( {{ request.GET.to }}, {{ request.GET.subject }}, {{ request.GET.message }} %}
P.S. I am new to django.
Upvotes: 2
Views: 1371
Reputation: 99680
This wont work. Django template is not designed to call python modules/methods directly. You can either create a template tag, or on click, trigger a url/view that would send the email.
From the philosophy of django template language
If you have a background in programming, or if you’re used to languages which mix programming code directly into HTML, you’ll want to bear in mind that the Django template system is not simply Python embedded into HTML. This is by design: the template system is meant to express presentation, not program logic.
Documentation on how to create template tags
And here is a nice tutorial talking about the email sending functionality through views.py
Upvotes: 3
Reputation: 174728
No, that's not possible. In order for those values request.GET.to
to be populated, you have to click the submit button - which has to go somewhere.
Now, if you really must - you can do this strictly from the browser using javascript and a library like mailgun which works over HTTP.
Simply create a request just like you would any AJAX request, read the contents of the form fields and your email message will be sent - without ever touching your server-side code.
Upvotes: 2