Python论坛  - 讨论区

标题:[python-chinese] permalink遇到的怪问题

2007年11月20日 星期二 09:05

John fractal johnfractal在gmail.com
星期二 十一月 20 09:05:12 HKT 2007

(1)情况大概这样的,我弄了一个django项目,其中urls.py有大概这么几句:
(r"^where/list/$",  "webui.views.where_list"),
(r"^where/detail/(?P.+)/$",  "webui.views.where_detail"),


(2)在webui/views.py里面这样定义
@login_required
def where_list(request):
    ...

@login_required
def where_detail(request, ...):
    ...

traceback.print_stack()
print id(where_detail)

(3)在templates用
{% url webui.views.where_list %}
可以正确生成
/where/list/

(4)在models里面这样定义:
class Where(models.Model):
    id = models.CharField(.........)
    ...

    @models.permalink
    def get_absolute_url(self):
        return ("webui.views.where_detail", (), {"where_id":self.id})

(5)但这个get_absolute_url()无法返回正确的url,总是出现NoReverseMatch异常。


(6)后来发现,程序运行后处理第一个请求时,打印了2次where_detail的id值,显然这个views被import了2次,居然是两个不同的id值:
140185036

140461068

也就是说,程序内部出现了2个where_detail的函数对象……

(7)两次print_stack()的结果都是相同:
  File
"/home/bullshit/workspace/django_svn/django/core/management/commands/runserver.py",
line 54, in inner_run
    run(addr, int(port), handler)
  File
"/home/bullshit/workspace/django_svn/django/core/servers/basehttp.py", line
651, in run
    httpd.serve_forever()
  File "SocketServer.py", line 201, in serve_forever
    self.handle_request()
  File "SocketServer.py", line 222, in handle_request
    self.process_request(request, client_address)
  File "SocketServer.py", line 241, in process_request
    self.finish_request(request, client_address)
  File "SocketServer.py", line 254, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File
"/home/bullshit/workspace/django_svn/django/core/servers/basehttp.py", line
543, in __init__
    BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
  File "SocketServer.py", line 522, in __init__
    self.handle()
  File
"/home/bullshit/workspace/django_svn/django/core/servers/basehttp.py", line
588, in handle
    handler.run(self.server.get_app())
  File
"/home/bullshit/workspace/django_svn/django/core/servers/basehttp.py", line
278, in run
    self.result = application(self.environ, self.start_response)
  File
"/home/bullshit/workspace/django_svn/django/core/servers/basehttp.py", line
620, in __call__
    return self.application(environ, start_response)
  File "/home/bullshit/workspace/django_svn/django/core/handlers/wsgi.py",
line 205, in __call__
    response = self.get_response(request)
  File "/home/bullshit/workspace/django_svn/django/core/handlers/base.py",
line 81, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File
"/home/bullshit/workspace/django_svn/django/contrib/auth/decorators.py",
line 62, in __call__
    return self.view_func(request, *args, **kwargs)
  File
"/home/bullshit/workspace/bukson_datain/../bukson_datain/datain/views.py",
line 74, in create_where
    return create_object(request, model=Where)
  File
"/home/bullshit/workspace/django_svn/django/views/generic/create_update.py",
line 50, in create_object
    return HttpResponseRedirect(new_object.get_absolute_url())
  File "/home/bullshit/workspace/django_svn/django/utils/functional.py",
line 3, in _curried
    return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
  File "/home/bullshit/workspace/django_svn/django/db/models/base.py", line
473, in get_absolute_url
    return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label,
opts.module_name), func)(self)
  File "/home/bullshit/workspace/django_svn/django/db/models/__init__.py",
line 32, in inner
    return reverse(bits[0], None, *bits[1:3])
  File "/home/bullshit/workspace/django_svn/django/core/urlresolvers.py",
line 301, in reverse
    return iri_to_uri(u'/' + get_resolver(urlconf).reverse(viewname, *args,
**kwargs))
  File "/home/bullshit/workspace/django_svn/django/core/urlresolvers.py",
line 279, in reverse
    lookup_view = get_callable(lookup_view, True)
  File "/home/bullshit/workspace/django_svn/django/utils/functional.py",
line 18, in wrapper
    result = func(*args)
  File "/home/bullshit/workspace/django_svn/django/core/urlresolvers.py",
line 47, in get_callable
    lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name)
  File
"/home/bullshit/workspace/bukson_datain/../bukson_datain/datain/views.py",
line 111, in 
    traceback.print_stack()

(8)跟踪permalink里面用到的revers函数,里面有一个views函数到url的revers_dict,通过对比id值,发觉它使用了第一次import时的where_detail对象。


疑问:
(a)为什么程序运行后,处理第一个请求时会出现了2个不同的views函数对象?
(b)没有没兄弟成功使用过这个函数……

我的环境是 kubuntu 7.10 + python 2.5.1 + django-svn今天check out的代码
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20071120/d0534ea1/attachment-0001.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月21日 星期三 13:10

@@ askfor在gmail.com
星期三 十一月 21 13:10:30 HKT 2007

def get_absolute_url(self):
²»ÊÇÓ¦¸Ã·µ»Øurl×Ö·û´®Âð
return 'where/detail/%s'  self.id ²»¿ÉÒÔÂð


On 11/20/07, John fractal <johnfractal在gmail.com> wrote:
>
> £¨1£©Çé¿ö´ó¸ÅÕâÑùµÄ£¬ÎÒŪÁËÒ»¸ödjangoÏîÄ¿£¬ÆäÖÐurls.pyÓдó¸ÅÕâô¼¸¾ä£º
> (r"^where/list/$",  "webui.views.where_list"),
> (r"^where/detail/(?P.+)/$",  "webui.views.where_detail "),
>
>
>
> £¨2£©ÔÚwebui/views.pyÀïÃæÕâÑù¶¨Òå
> @login_required
> def where_list(request):
>     ...
>
> @login_required
> def where_detail(request, ...):
>     ...
>
>
> traceback.print_stack()
> print id(where_detail)
>
>
> £¨3£©ÔÚtemplatesÓÃ
> {% url webui.views.where_list %}
>
> ¿ÉÒÔÕýÈ·Éú³É
> /where/list/
>
>
> £¨4£©ÔÚmodelsÀïÃæÕâÑù¶¨Ò壺
> class Where(models.Model):
>     id = models.CharField(.........)
>     ...
>
>     @models.permalink
>     def get_absolute_url(self):
>         return ("webui.views.where_detail", (), {"where_id":self.id})
>
>
> £¨5£©µ«Õâ¸öget_absolute_url()ÎÞ·¨·µ»ØÕýÈ·µÄurl£¬×ÜÊdzöÏÖNoReverseMatchÒì³£¡£
>
>
> £¨6£©ºóÀ´·¢ÏÖ£¬³ÌÐòÔËÐкó´¦ÀíµÚÒ»¸öÇëÇóʱ£¬´òÓ¡ÁË2´Îwhere_detailµÄidÖµ£¬ÏÔÈ»Õâ¸öviews±»importÁË2´Î£¬¾ÓÈ»ÊÇÁ½¸ö²»Í¬µÄidÖµ:
>
> 140185036
>
> 140461068
>
>
> Ò²¾ÍÊÇ˵£¬³ÌÐòÄÚ²¿³öÏÖÁË2¸öwhere_detailµÄº¯Êý¶ÔÏó¡­¡­
>
> £¨7£©Á½´Îprint_stack()µÄ½á¹û¶¼ÊÇÏàͬ£º
>   File
> "/home/bullshit/workspace/django_svn/django/core/management/commands/runserver.py",
> line 54, in inner_run
>     run(addr, int(port), handler)
>   File
> "/home/bullshit/workspace/django_svn/django/core/servers/basehttp.py", line
> 651, in run
>     httpd.serve_forever()
>   File "SocketServer.py", line 201, in serve_forever
>     self.handle_request()
>   File "SocketServer.py", line 222, in handle_request
>     self.process_request(request, client_address)
>   File "SocketServer.py", line 241, in process_request
>     self.finish_request(request, client_address)
>   File "SocketServer.py", line 254, in finish_request
>     self.RequestHandlerClass(request, client_address, self)
>   File
> "/home/bullshit/workspace/django_svn/django/core/servers/basehttp.py", line
> 543, in __init__
>     BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
>   File "SocketServer.py", line 522, in __init__
>     self.handle()
>   File
> "/home/bullshit/workspace/django_svn/django/core/servers/basehttp.py", line
> 588, in handle
>     handler.run(self.server.get_app())
>   File
> "/home/bullshit/workspace/django_svn/django/core/servers/basehttp.py", line
> 278, in run
>     self.result = application(self.environ, self.start_response)
>   File
> "/home/bullshit/workspace/django_svn/django/core/servers/basehttp.py", line
> 620, in __call__
>     return self.application(environ, start_response)
>   File "/home/bullshit/workspace/django_svn/django/core/handlers/wsgi.py",
> line 205, in __call__
>     response = self.get_response(request)
>   File "/home/bullshit/workspace/django_svn/django/core/handlers/base.py",
> line 81, in get_response
>     response = callback(request, *callback_args, **callback_kwargs)
>   File
> "/home/bullshit/workspace/django_svn/django/contrib/auth/decorators.py",
> line 62, in __call__
>     return self.view_func(request, *args, **kwargs)
>   File
> "/home/bullshit/workspace/bukson_datain/../bukson_datain/datain/views.py",
> line 74, in create_where
>     return create_object(request, model=Where)
>   File
> "/home/bullshit/workspace/django_svn/django/views/generic/create_update.py",
> line 50, in create_object
>     return HttpResponseRedirect(new_object.get_absolute_url())
>   File "/home/bullshit/workspace/django_svn/django/utils/functional.py",
> line 3, in _curried
>     return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
>   File "/home/bullshit/workspace/django_svn/django/db/models/base.py",
> line 473, in get_absolute_url
>     return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label,
> opts.module_name), func)(self)
>   File "/home/bullshit/workspace/django_svn/django/db/models/__init__.py",
> line 32, in inner
>     return reverse(bits[0], None, *bits[1:3])
>   File "/home/bullshit/workspace/django_svn/django/core/urlresolvers.py",
> line 301, in reverse
>     return iri_to_uri(u'/' + get_resolver(urlconf).reverse(viewname,
> *args, **kwargs))
>   File "/home/bullshit/workspace/django_svn/django/core/urlresolvers.py",
> line 279, in reverse
>     lookup_view = get_callable(lookup_view, True)
>   File "/home/bullshit/workspace/django_svn/django/utils/functional.py",
> line 18, in wrapper
>     result = func(*args)
>   File "/home/bullshit/workspace/django_svn/django/core/urlresolvers.py",
> line 47, in get_callable
>     lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name)
>   File
> "/home/bullshit/workspace/bukson_datain/../bukson_datain/datain/views.py",
> line 111, in 
>     traceback.print_stack()
>
>
> £¨8£©¸ú×ÙpermalinkÀïÃæÓõ½µÄreversº¯Êý£¬ÀïÃæÓÐÒ»¸öviewsº¯Êýµ½urlµÄrevers_dict£¬Í¨¹ý¶Ô±ÈidÖµ£¬·¢¾õËüʹÓÃÁ˵ÚÒ»´ÎimportʱµÄwhere_detail¶ÔÏó¡£
>
>
>
> ÒÉÎÊ£º
> £¨a£©ÎªÊ²Ã´³ÌÐòÔËÐк󣬴¦ÀíµÚÒ»¸öÇëÇóʱ»á³öÏÖÁË2¸ö²»Í¬µÄviewsº¯Êý¶ÔÏó£¿
> £¨b£©Ã»ÓÐûÐֵܳɹ¦Ê¹ÓùýÕâ¸öº¯Êý¡­¡­
>
> ÎҵĻ·¾³ÊÇ kubuntu 7.10 + python 2.5.1 + django-svn½ñÌìcheck outµÄ´úÂë
>
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071121/5565654a/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号