ssharma
ssharma

Reputation: 349

how to redirect in Django correctly?

There is a button in the profile page in my django application,after clicking that button,I want to redirect it to my home page.

On the click of that button,a function in views.py is mapped which is working fine and the function is:

    @require_http_methods(['GET','POST'])
    def permanentblock(request,Username):
        blocker = MyUser.objects.get(username=request.user.username)
        blocked = MyUser.objects.get(username=Username)
        a = bl_.objects.create(blocker=blocker,blocked=blocked,myboolean=True)
        redirect(reverse('home',kwargs={'id':request.user.id}));
#there is a entry in my urls.py for 'home' variable along with the parameter.

Now,till the second last line of the function,it is working correctly,the table is updated.But, there is an error shown:

ValueError at /account/permanentblock/insta/
The view account.views.permanentblock didn't return an HttpResponse object. It returned None instead.

Now,I don't want to return anything from this function,I just want a re-direction. Isn't it the correct way of redirecting to a html page?

Please provide suggestions on how to correct this implementation.

Thanks.

Upvotes: 0

Views: 160

Answers (1)

Exprator
Exprator

Reputation: 27503

i dont know why you are using ; at the end? anyways use this

@require_http_methods(['GET','POST'])
def permanentblock(request,Username):
    blocker = MyUser.objects.get(username=request.user.username)
    blocked = MyUser.objects.get(username=Username)
    a = bl_.objects.create(blocker=blocker,blocked=blocked,myboolean=True)
    return redirect(reverse('home',kwargs={'id':request.user.id}))

Upvotes: 1

Related Questions