reverse():'NoneType'对象不可调用

时间:2014-02-22 07:59:59

标签: python django

我面临一个奇怪的问题,至少对于一个python / django noob来说这很奇怪;)

从我的 models.py 调用

django.core.urlresolvers.reverse()引发'NoneType'对象无法调用。当我从shell执行相同的行时,它工作正常。

我完全不知道为什么会这样。

贝壳:

$ from django.core.urlresolvers import reverse
$ reverse('images:view', args=[1])
'/images/1/'

models.py

from django.db import models
from django.core.urlresolvers import reverse

class Image(models.Model):
    image = models.ImageField(upload_to='images')
    image_caption = models.CharField(max_length=80, verbose_name='Caption')

    uploaded_at = models.DateTimeField(auto_now_add=True, verbose_name='Date Uploaded')
    modified_at = models.DateTimeField(auto_now=True, verbose_name='Date Modified')

    def __str__(self):
        return self.image_caption

    def edit_image(self):
        return '<a href="%s">Edit</a>' % reverse('images:view', args=[self.id])

    edit_image.short_description = 'Edit Image'
    edit_image.allow_tags = True

admin.py 在此处重新edit_image

from django.contrib import admin
from images.models import Image

class ImageAdmin(admin.ModelAdmin):
    search_fields = ['image_caption']
    list_display = ['id', 'image_caption', 'edit_image']

admin.site.register(Image, ImageAdmin)

url我试图在图片命名空间内反转

url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='view'),

我尝试了什么?

  • reverse_lazy()而不是rev​​erse()
  • kwargs而不是arg
  • 和我甚至不知道的事情。 :P

TRACEBACK

Template error:
In template /home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/contrib/admin/templates/admin/change_list.html, error at line 91
   'NoneType' object is not callable
   81 :         {% endif %}
   82 :       {% endblock %}
   83 :
   84 :       <form id="changelist-form" action="" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %}>{% csrf_token %}
   85 :       {% if cl.formset %}
   86 :         <div>{{ cl.formset.management_form }}</div>
   87 :       {% endif %}
   88 : 
   89 :       {% block result_list %}
   90 :           {% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}
   91 :            {% result_list cl %} 
   92 :           {% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}
   93 :       {% endblock %}
   94 :       {% block pagination %}{% pagination cl %}{% endblock %}
   95 :       </form>
   96 :     </div>
   97 :   </div>
   98 : {% endblock %}
   99 : 

Traceback:
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/core/handlers/base.py" in get_response
  139.                 response = response.render()
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/response.py" in rendered_content
  82.         content = template.render(context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/template/base.py" in render
  1196.                     _dict = func(*resolved_args, **resolved_kwargs)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/contrib/admin/templatetags/admin_list.py" in result_list
  288.             'results': list(results(cl))}
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/contrib/admin/templatetags/admin_list.py" in results
  266.             yield ResultList(None, items_for_result(cl, res, None))
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/contrib/admin/templatetags/admin_list.py" in __init__
  258.         super(ResultList, self).__init__(*items)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/contrib/admin/templatetags/admin_list.py" in items_for_result
  185.             f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "/home/ankit/Desktop/Python/Workspace/VirtualEnv3.3.2/local/lib/python3.3/dist-packages/django/contrib/admin/util.py" in lookup_field
  258.                 value = attr()
File "/home/ankit/Desktop/Python/Workspace/SnapShare/images/models.py" in edit_image
  30.         return '<a href="%s">Edit</a>' % reverse('images:view', args=[self.id])

Exception Type: TypeError at /admin/images/image/
Exception Value: 'NoneType' object is not callable

Python版本: 3.3.2

Django版本: 1.6.2

提前致谢:)

1 个答案:

答案 0 :(得分:0)

解决方案1 ​​

在模型的方法中使用reverse_lazy应该可以解决问题(即使你尝试过)。这应该允许您在最终加载URL时使用callable。

解决方案2

将模型方法移动到ModelAdmin方法,这里是“copy-paste-ready”片段:

class ImageAdmin(admin.ModelAdmin):

    # ... the rest of the class ... #

    def edit_image(self, obj):
        return '<a href="%s">Edit</a>' % reverse('images:view', args=[obj.id])

    edit_image.short_description = 'Edit Image'
    edit_image.allow_tags = True

问题

在模型中使用reverse可能是导致此行为的原因。

说明

模型可能过早加载(在urlconf完成之前),因此反向方法尚未定义。