views:

35

answers:

1

hello, I'm making a simple vote class, where a user can vote up or vote down an answer. I don't want this function to return anything,as the vote for every user is created when he votes. The problem is that my redirection, or empty return gives me an error like: The page isn't redirecting properly from browser.

My code:

def vote_answer_down(request,id):
   answer = Answer.objects.get(pk = id)
   VoteDownAnswer.objects.create(answer = answer, voted_down_by = request.user) 

   return HttpResponseRedirect('.') #or return (without httpresponse),gives the same

where am i wrong? Thanks!

+1  A: 

http://docs.djangoproject.com/en/1.2/ref/request-response/#django.http.HttpResponseRedirect

The constructor takes a single argument -- the path to redirect to. This can be a fully qualified URL (e.g. 'http://www.yahoo.com/search/') or an absolute URL with no domain (e.g. '/search/'). Note that this returns an HTTP status code 302.

What makes you think '.' will work?

Usually use the reverse function to provide the absolute URL required.

http://docs.djangoproject.com/en/dev/topics/http/urls/#reverse

S.Lott
it doesn' work neither if i put www.google.com there.. may it be because i'm creating the vote object there? i just want it to remain in the current page
dana
www.google.com wouldn't work either, but try putting in "http://google.com/"or even just "/" for your homepage
jturnbull
@dana: "may it be because i'm creating the vote object there?" No. The `return` object is the `return` object. If it won't redirect, you're clearly doing something completely wrong in Django or mod_wsgi or Apache. Since you haven't provided much detail on your technology stack, we're left to guess.
S.Lott
yes, it actually works if i put '/' or a relative path to one of my functions. i'll just put the relative path to my homepage, then!Thanks so much for answers!
dana
@dana: A relative path is the wrong thing to do. I tried to be very clear. You **must** use the `reverse` function to generate the URL within your application.
S.Lott
:) ok, then, i'll use the reverse function:)
dana