reference : http://stackoverflow.com/questions/28400943/python-django-e-mail-form-example
기본 구조 이해에 도움이 많이 되서 저장해둠
A simple example could be:
urls.py
from django.conf.urls import patterns, url
from yourapp import views
urlpatterns = patterns(
'',
url(r'^email/$',
views.email,
name='email'
),
url(r'^thanks/$',
views.thanks,
name='thanks'
),
)
forms.py
from django import forms
class ContactForm(forms.Form):
from_email = forms.EmailField(required=True)
subject = forms.CharField(required=True)
message = forms.CharField(widget=forms.Textarea)
views.py
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from yourapp.forms import ContactForm
def email(request):
if request.method == 'GET':
form = ContactForm()
else:
form = ContactForm(request.POST)
if form.is_valid():
subject = form.cleaned_data['subject']
from_email = form.cleaned_data['from_email']
message = form.cleaned_data['message']
try:
send_mail(subject, message, from_email, ['admin@example.com'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('thanks')
return render(request, "yourapp/email.html", {'form': form})
def thanks(request):
return HttpResponse('Thank you for your message.')
email.html
<form method="post">
{% csrf_token %}
{{ form }}
<div class="form-actions">
<button type="submit">Send</button>
</div>
</form>
반응형
'Language > PYTHON' 카테고리의 다른 글
[라즈베리파이3] NOOBS를 이용하여 OS 설치하기 (Windows7 에서 작업함) (8) | 2017.04.04 |
---|---|
python3 beautifulsoup 한글 깨짐 (9) | 2017.02.16 |
[Python3] konlpy 설치시 jpype 관련 실패 해결 방안 (9) | 2017.02.12 |
Python3에서 한글 깨짐 없이 gmail 전송 (13) | 2017.01.03 |
ImportError: No module named BeautifulSoup 에러 (8) | 2015.06.26 |