0
我有一個團隊註冊應用程序。用戶註冊成爲團隊的一員。當他們這樣做時,視圖添加一個團隊成員並將用戶添加到auth數據庫中。同時創建用戶註冊和Django用戶。保存方法是添加用戶,無論有效的形式
但是,當團隊模型返回無效數據時,user.save()方法添加用戶名,密碼和電子郵件。
因此,當用戶更正團隊註冊並點擊提交時,他們現在被告知用戶名已被提取。
我想要發生的是用戶名等被添加後,團隊成員已驗證。
有什麼建議嗎?
這裏的觀點:
def register(request, **kwargs):
if request.method == 'POST':
form = SimpleRegForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
email = form.cleaned_data['email']
password = form.cleaned_data['password1']
user = User.objects.create_user(username, email, password)
user.save()
form.save()
# Send an email to the admin letting them know that a person registered
subject = render_to_string('teamregistration/email_subject.txt')
message = render_to_string('teamregistration/email_body.txt',
{'first_name':form.cleaned_data['first_name'], 'last_name':form.cleaned_data['last_name'], 'tbi_region':form.cleaned_data['tbi_region']})
recipients = ['[email protected]', '[email protected]']
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipients)
return HttpResponseRedirect('/tbi-team-register/registration-submitted/')
else:
form = SimpleRegForm()
return render_to_response('teamregistration/form.html', {'form':form}, context_instance=RequestContext(request))
我沒有看到在這個例子中完成了團隊成員處理,只處理了用戶註冊表單(並且出現了兩次)。這在我看來是一個簡單的優先問題:確保兩者的數據都是正確的,然後保存兩者。如果你不能這樣做,你可能需要考慮Django的「Commit On Success」裝飾器,如果任何一個事務失敗,這將導致給定視圖內的所有事務自動回滾。 – 2010-11-01 16:22:36
這是一個更優雅的答案。但是......我是。我所要做的就是刪除user.save()。這解決了問題,限制了保存到form.save() – 2010-11-03 04:50:17