Reputation: 311
I have written a function where the function captures the details from a form and sends an email after form submission. How can I have this functionality rendered to multiple django templates where i can call the form and do so. Below is the related function..
def emailView(request):
if request.method == 'GET':
form = myform()
else:
form = myform(request.POST)
if form.is_valid():
subject='form Details'
mobile = form.cleaned_data['mobile']
email = form.cleaned_data['email']
dummy = '\nMobile: '+mobile+'\nEmail: '+email'
try:
send_mail(subject, dummy, '[email protected]', ['[email protected]', '[email protected]'])
messages.success( request, " Thank you !! For contacting.')
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('email')
return render(request, "my_app/email.html", {'form': form})
Upvotes: 2
Views: 48
Reputation: 849
You can use include tag to do so. Try out the below way.
consider the below as your url in urls.py
path('your_form/', views.emailView, name='myform'),
you can call your function any number of templates using the below tag.
#template1.html
<form method="post" action="{% url 'myform' %}">
{% include 'yourapp/email.html' %}
<button type="submit">Submit</button>
#template2.html
<form method="post" action="{% url 'myform' %}">
{% include 'yourapp/email.html' %}
<button type="submit">Submit</button>
Upvotes: 2