HTTP_REFERER

282 views
Skip to first unread message

PythonistL

unread,
Nov 5, 2005, 6:04:27 AM11/5/05
to Django users
I use the following view for user's log in
###################
def MyLogin(request):
WrongID=0
AllGood=1
if request.POST:
print "Refferer1 from POST",Refferer1
try:
u=users.get_object(Login__exact=request.POST['Login'])
except users.UserDoesNotExist:
WrongID=1
if not WrongID and (u.Password==request.POST['Password']):
return render_to_response('board/SuccessfulLogin', {'u':
u})
else:
return HttpResponse("Wrong ID or a password.")
else:#first GET the LoginForm
Refferer1= request.META['HTTP_REFERER']
print "Refferer1 from GET",Refferer1
manipulator = users.AddManipulator()
errors = new_data = {}
form = formfields.FormWrapper(manipulator, new_data,
errors)
return render_to_response('board/LoginForm', {'form':form})
###################
and I would like to check 'HTTP_REFERER' so I use
Refferer1= request.META['HTTP_REFERER']
on line 15.
I want to use the same 'HTTP_REFERER' value I got from GET, in POST
part too ( line 5), but it is empty on line 5. The value is not saved
from GET ( line 15).
Can anybody explain WHY the value from GET part
(command on line 15 such as
Refferer1= request.META['HTTP_REFERER']
)
is not saved to POST?
Thank you
L.

Andreas Stuhlmüller

unread,
Nov 5, 2005, 7:05:53 AM11/5/05
to Django users
PythonistL wrote:
> Can anybody explain WHY the value from GET part
> (command on line 15 such as
> Refferer1= request.META['HTTP_REFERER']
> ) is not saved to POST?

request.POST contains only the variables POSTed by the user. You'll
have to make sure that the referer variable is part of your form if you
want to use it after form submission.

You could, for example, change your last line to

return render_to_response('board/LoginForm',
{'form':form, 'ref':request.META['HTTP_REFERER']})

and render {{ ref }} as the value of a hidden input field in your login
form. Thus request.POST['ref'] will contain the referer.

Andreas

Luke Plant

unread,
Nov 5, 2005, 8:53:18 AM11/5/05
to django...@googlegroups.com
On Sat, 05 Nov 2005 03:04:27 -0800 PythonistL wrote:

>
> I use the following view for user's log in
> ###################
> def MyLogin(request):
> WrongID=0
> AllGood=1
> if request.POST:
> print "Refferer1 from POST",Refferer1

At this point, you haven't yet set Referrer1, so it will throw an
exception. I've tested it and request.META['HTTP_REFERER'] always does
have the referer as it ought, but if there is no referer header it will
throw a KeyError, so you need to do something like this:

referer = request.META.get('HTTP_REFERER', None)

You should also note that the header can be forged, and some people
have it turned off for privacy reasons, so I wouldn't rely on it being
there. It is not reliably present, and not reliable if present.

Luke


--
"I regret I wasn't born with opposable toes." (Calvin and Hobbes)

Luke Plant || L.Plant.98 (at) cantab.net || http://lukeplant.me.uk/

PythonistL

unread,
Nov 7, 2005, 3:09:15 AM11/7/05
to Django users
Thank you Andreas and Luke for help and explanation
Regards,
L.

Reply all
Reply to author
Forward
0 new messages