我有以下视图代码:
def activate( request = '', actkey = "" ):
message = ""
if len( actkey ) != 40:
message += str( len(actkey))
if request.method == 'POST':
form = ActivateForm( request.POST )
if form.is_valid():
actkey = request.POST['actkey']
activate( '', actkey )
else:
form = ActivateForm()
else:
profile = userprofile.objects.get( actkey = actkey )
user = User.objects.get( id = profile.user_id )
user.is_active = True
user.save()
profile.actkey = ""
profile.save()
message += "Uw account is succesvol geactiveerd."
return render_to_response( 'profile/register.html', { 'message' : message } )
return render_to_response( 'profile/register.html', { 'message' : message, 'form' : form } )
它的作用很简单,当通过URL给出激活密钥时,它会转到:
profile = userprofile ....等
但是如果没有给它加载django表单,那么用户就可以键入它的激活码(到目前为止一切正常)
但是当用户发布他的激活密钥时,它会出现在if len( actkey ) != 40:
这不应该发生,因为激活密钥是40 ...但由于它再次显示表单,但是激活用户以及我得到不需要的行为......
我该如何解决这个问题?
感谢您的帮助
答案 0 :(得分:1)
您应该在成功提交表单后重定向
if request.method == 'POST':
form = ActivateForm( request.POST )
if form.is_valid():
actkey = form.cleaned_data['actkey']#access cleaned_data instead of raw post
activate( '', actkey )
return HttpResponseRedirect('/')
答案 1 :(得分:0)
这不是我想要的,但我用这种方式修复了它:
def activate( request = '', actkey = "" ):
message = ""
if len( actkey ) != 40:
if request.method == 'POST':
form = ActivateForm( request.POST )
if form.is_valid():
actkey = request.POST['actkey']
profile = userprofile.objects.get( actkey = actkey )
user = User.objects.get( id = profile.user_id )
user.is_active = True
user.save()
profile.actkey = ""
profile.save()
message += "Uw account is succesvol geactiveerd."
return render_to_response( 'profile/register.html', { 'message' : message } )
else:
form = ActivateForm()
else:
profile = userprofile.objects.get( actkey = actkey )
user = User.objects.get( id = profile.user_id )
user.is_active = True
user.save()
profile.actkey = ""
profile.save()
message += "Uw account is succesvol geactiveerd."
return render_to_response( 'profile/register.html', { 'message' : message } )
return render_to_response( 'profile/register.html', { 'message' : message, 'form' : form } )
感谢您的回复:)
答案 2 :(得分:-1)
我可能会误解你的意图。 但我认为当表格有效时,你应该做类似的事情:
if form.is_valid():
actkey = request.POST['actkey']
activate( '', actkey )
return render_to_response( 'profile/register.html', { 'message' : message } )
即。激活用户后省略表单对象。