视图.py
from registration.models import Registration
from django.core.context_processors import csrf
from registration import tasks
from django.shortcuts import render_to_response
def validate(request):
state="notvalid"
if request.method == 'POST':# If the form has been submitted...
form = Registration(request.POST) # A form bound to the POST data
state="notvalid"
if form.is_valid():
username = form.cleaned_data['username']
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
email = form.cleaned_data['email']
password = form.cleaned_data['password']
from django.contrib.auth.models import User
user = User.objects.create_user(username,email,password)
user.first_name=first_name
user.last_name=last_name
user.save()
state="added data"
tasks.mail()
else:
form = Registration() # An unbound form
return render_to_response('auth1.html', {
'form': form,'state':state,
})
任务.py
from celery import task
import celery
@celery.task()
def mail():
from django.core.mail import send_mail
for x in range(10000000):
print x
return True
该任务必须在不影响视图响应时间的情况下并行执行。即在后台它必须运行邮件功能
但是在数据库添加它之后返回render_to_response