I go an error with"refer = web.ctx.env["HTTP_REFERER"]"

64 views
Skip to first unread message

大郎

unread,
May 1, 2009, 1:03:13 PM5/1/09
to we...@googlegroups.com
in web.py 0.31, int windows, python 2.5.4

it raise:

G:\do\alldo\autopart007\web>python code.py
Traceback (most recent call last):
  File "code.py", line 4, in <module>
    from controllers import admin, people
  File "G:\do\alldo\autopart007\web\controllers\people.py", line 3, in <module>
    import web, md5, settings
  File "G:\do\alldo\autopart007\web\settings.py", line 7, in <module>
    refer = web.ctx.env["HTTP_REFERER"]
  File "D:\Python\lib\site-packages\web\utils.py", line 830, in __getattr__
    return getattr(self._getd(), key)
  File "D:\Python\lib\site-packages\web\utils.py", line 64, in __getattr__
    raise AttributeError, k
AttributeError: 'env'

I had read the web.py - application.py, it look like right.

        The headers argument specifies HTTP headers as a mapping object
        such as a dict.

            >>> urls = ('/ua', 'uaprinter')
            >>> class uaprinter:
            ...     def GET(self):
            ...         return 'your user-agent is ' + web.ctx.env['HTTP_USER_AGENT']
            ...

--
百才招聘(baicai.com).产品部
Email: k...@baicai.com
Gtalk: qicha...@gmail.com
twitter.com/qichangxing

Sherwin Soltani

unread,
May 1, 2009, 1:19:12 PM5/1/09
to web.py
> > Traceback (most recent call last):
> >   File "code.py", line 4, in <module>
> >     from controllers import admin, people
> >   File "G:\do\alldo\autopart007\web\controllers\people.py", line 3, in
> > <module>
> >     import web, md5, settings
> >   File "G:\do\alldo\autopart007\web\settings.py", line 7, in <module>
> >     refer = web.ctx.env["HTTP_REFERER"]
> >   File "D:\Python\lib\site-packages\web\utils.py", line 830, in __getattr__
> >     return getattr(self._getd(), key)
> >   File "D:\Python\lib\site-packages\web\utils.py", line 64, in __getattr__
> >     raise AttributeError, k
> > AttributeError: 'env'

There is an uncaught exception in getting the HTTP_REFERER variable.
Chances are that this variable hasn't been set.

Wrap the code in a try:...except: block and write something that deals
with the lack of that variable's existence, and that should solve your
problem.

Anand Chitipothu

unread,
May 1, 2009, 11:16:14 PM5/1/09
to we...@googlegroups.com
>> G:\do\alldo\autopart007\web>python code.py
>> Traceback (most recent call last):
>> File "code.py", line 4, in <module>
>> from controllers import admin, people
>> File "G:\do\alldo\autopart007\web\controllers\people.py", line 3, in
>> <module>
>> import web, md5, settings
>> File "G:\do\alldo\autopart007\web\settings.py", line 7, in <module>
>> refer = web.ctx.env["HTTP_REFERER"]
>> [..]

>> raise AttributeError, k
>> AttributeError: 'env'

web.ctx.env is available only when handling requests. Looks like you
are using it when initializing the application.

大郎

unread,
May 1, 2009, 11:50:18 PM5/1/09
to we...@googlegroups.com
On Sat, May 2, 2009 at 11:16 AM, Anand Chitipothu <anand...@gmail.com> wrote:

web.ctx.env is available only when handling requests. Looks like you
are using it when initializing the application.


yes, I think so.
I use it in settings.py, when I need it:

import settings
return settings.refer

so...

if this referer is null, can it be pass?

Anand Chitipothu

unread,
May 2, 2009, 12:12:17 AM5/2/09
to we...@googlegroups.com
2009/5/2 大郎 <qicha...@gmail.com>:

> On Sat, May 2, 2009 at 11:16 AM, Anand Chitipothu <anand...@gmail.com>
> wrote:
>>
>> web.ctx.env is available only when handling requests. Looks like you
>> are using it when initializing the application.
>
>
> yes, I think so.
> I use it in settings.py, when I need it:

You can directltt use web.ctx.HTTP_REFERRER wherever needed.
But if you want to use if only from setttings.py then write a function
that returns that

in settings.py:

def referrer():
return web.ctx.HTTP_REFERRER

and use

settings.referrer()

Reply all
Reply to author
Forward
0 new messages