环境: python 2.7 32位,Django1.6.7,win7 64位系统
承接前3篇文章
设计表单页面:contact_form.html
<html> <head> <title>Contact us</title> </head> <body> <h1>Contact us</h1> {% if errors %} <ul> {% for error in errors %} <li>{{ error }} </li> {% endfor %} </ul> {% endif %} <form action="/contact/" method="post" > {% csrf_token %} <p>Subject:<input type="text" name="subject"></p> <p>Your e-mail(optional):<input type="text" name="email"</p> <p>Message:<textarea name="message" rows="10" cols="50"></textarea></p> <input type="submit" value="Submit"> </form> </body> </html>
视图views.py:定义了查找页面search、表单页面contact
from django.http import HttpResponse from django.shortcuts import render_to_response from books.models import Book from django.template import RequestContext from django.core.mail import send_mail from django.http import HttpResponseRedirect def hello(request): return HttpResponse("hello world!") def search(request): errors = [] if 'q' in request.GET : q = request.GET['q'] if not q: errors.append('Enter a search term.') elif len(q) >20: errors.append('Please enter at most 20 characters.') else: books = Book.objects.filter(title__icontains=q) return render_to_response('search_result.html', {'books':books,'query':q}) return render_to_response('search_form.html',{'errors':errors}) def contact(request): # title='This is mail title.' # testmessage='Hello, This is a message' # testsender='[email protected]' # testmail_list=['[email protected]',] # send_mail( # subject=title, # message=testmessage, # from_email=testsender, # recipient_list=testmail_list, # fail_silently=False, # connection=None # ) errors=[] if request.method == 'POST': if not request.POST.get('subject',''): errors.append('Enter a subject.') if not request.POST.get('message',''): errors.append('Enter a message.') if request.POST.get('email') and '@' not in request.POST['email']: errors.append('Enter a valid e-mail address.') if not errors: send_mail( request.POST['subject'], request.POST['message'], request.POST.get('email','[email protected]'), ['[email protected]'], ) return HttpResponseRedirect('/contact/thanks/') return render_to_response('contact_form.html', {'errors':errors, 'subject': request.POST.get('subject',''), 'message': request.POST.get('message',''), 'email': request.POST.get('email',''), }, context_instance=RequestContext(request)) def thanks(request): return HttpResponse("Great Thanks!")
CSRF verification failed. Request aborted.
Reason given for failure:
CSRF token missing or incorrect.
In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
RequestContext
for the template, instead of Context
.{% csrf_token %}
template tag inside each POST form that targets an internal URL.CsrfViewMiddleware
, then you must use csrf_protect
on any views that use the csrf_token
template tag, as well as those that accept the POST data. You're seeing the help section of this page because you have DEBUG = True
in your Django settings file. Change that to False
, and only the initial error message will be displayed.
You can customize this page using the CSRF_FAILURE_VIEW setting.
解决办法:http://blog.csdn.net/chenggong2dm/article/details/7729576
发送邮件:
1、导入send_mail
2、在setting中设置邮件账号和密码
如:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.qq.com'
EMAIL_PORT = 25
EMAIL_HOST_USER='123456@qq.com'
EMAIL_HOST_PASSWORD='123456'
3、测试
用上面的contact的例子,启动python manage runserver,然后在浏览器输入http://127.0.0.1:8000/contact/,填写subject和message两个内容区域,,点击submit按钮,即可在[email protected]中查看邮件了