4

I have the following view code:

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 } )

What it does is simple, when the activation key is given through the URL, it goes to: profile = userprofile.... etc. But when it's not given it loads the django form so the user can type it's activation code (all works well so far) But when the user posts his activation key it comes in the if len( actkey ) != 40: That shouldn't happen because the activation key is 40... But since it shows the form again, but activates the user as well i get unwanted behavior...

How can i fix this?

Thanks for the help

4 Answers 4

6

You should redirect after a successful form submission

 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('/')
Sign up to request clarification or add additional context in comments.

Comments

4

The best way to solve this problem is using a redirect to the previous page:

return redirect(request.META['HTTP_REFERER'])

Comments

0

It's both not quite what i was looking for, but i fixed it this way:

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 } )

Thanks for the replies :)

Comments

-1

I may misunderstand your intension. But I think when the form is valid you should do something like:

if form.is_valid():
    actkey = request.POST['actkey']
    activate( '', actkey )
    return render_to_response( 'profile/register.html', { 'message' : message } )

ie. omit the form object after you activate the user.

1 Comment

This function preceded the introduction of render() and works similarly except that it doesn’t make the request available in the response. It’s not recommended and is likely to be deprecated in the future. check documentation here: docs.djangoproject.com/en/1.11/topics/http/shortcuts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.