Make the Contact form in django and also mail is send to admin .
forms.pyfrom django import forms
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
email = forms.EmailField(required=False, label='Your e-mail address')
message = forms.CharField(widget=forms.Textarea)
View.py
from django.core.mail import send_mail
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from mysite.contact.forms import *
from django.template import RequestContext
from django.core.urlresolvers import reverse
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
send_mail(
cd['subject'],
cd['message'],
cd.get('email', 'noreply@example.com'),
['singh.malhi22@gmail.com'],
)
return render_to_response('contact/thanks.html', {'form': form}, context_instance=RequestContext(request))
else:
form = ContactForm()
return render_to_response('contact/contact.html', {'form': form}, context_instance=RequestContext(request))
Template used :
output.html
<html>
<head>
<title>Contact us</title>
</head>
<body>
<link rel="stylesheet" type="text/css" href="/media/css/base.css" />
<h1>Contact us</h1>
<br>
{% if form.errors %}
<p style="color: red;">
Please correct the error{{ form.errors|pluralize }} below.
</p>
{% endif %}
<form action="" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
thanks.html
<html>
<head>
<title>Thanks</title>
</head>
<link rel="stylesheet" type="text/css" href="/media/css/base.css" />
<body>
<h1>Thanks</h1>
<br>
<h3>
You are in safe hands</h3>
</body>
No comments:
Post a Comment