python - 使用 get_absolute_url() 时出现 NoReverseMatch 错误

标签 python django python-2.7 django-urls

我正在尝试使用 get_absolute_url 来遵循 DRY 规则。如果我对类进行编码以直接从 slug 构建 href,则一切正常。丑陋,凌乱,但工作......

因此,我尝试使用 get_absolute_url() 正确完成此操作,但使用下面的代码却遇到了 NoReverseMatch 异常。我知道这一定是某种新手错误,但我已经在所有文档和论坛上翻阅了好几天了,但仍然无法弄清楚这个问题!

我收到此错误:

NoReverseMatch at /calendar
Reverse for 'pEventsCalendarDetail' with arguments '()' and keyword arguments '{u'slug': u'Test-12014-05-05'}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL:    http://127.0.0.1:8000/calendar
Django Version: 1.6
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'pEventsCalendarDetail' with arguments '()' and keyword arguments '{u'slug': u'Test-12014-05-05'}' not found. 0 pattern(s) tried: []
Exception Location: /usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 429
Python Executable:  /usr/local/opt/python/bin/python2.7
Python Version: 2.7.6

使用以下 models.py 摘录:

@python_2_unicode_compatible
class Event(models.Model):
    eventName = models.CharField(max_length=40)
    eventDescription = models.TextField()
    eventDate = models.DateField()
    eventTime = models.TimeField()
    eventLocation = models.CharField(max_length=60, null=True, blank=True)
    creationDate = models.DateField(auto_now_add=True)
    eventURL = models.URLField(null=True, blank=True)
    slug = AutoSlugField(populate_from=lambda instance: instance.eventName + str(instance.eventDate),
                         unique_with=['eventDate'],
                         slugify=lambda value: value.replace(' ','-'))

    @models.permalink
    def get_absolute_url(self):
        from django.core.urlresolvers import reverse
        path = reverse('pEventsCalendarDetail', (), kwargs={'slug':self.slug})
        return "http://%s" % (path)

完整的 urls.py 文件:

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'electricphoenixfll.views.home', name='home'),
    url(r'^home$', 'electricphoenixfll.views.home', name='home'),
    url(r'^calendar$', 'electricphoenixfll.views.calendar', name='calendar'),
    url(r'^forum$', 'electricphoenixfll.views.forum', name='forum'),
    url(r'^donate$', 'electricphoenixfll.views.donate', name='donate'),
    url(r'^donate_thanks$', 'electricphoenixfll.views.donate_thanks', name='donate_thanks'),
    url(r'^what_is_fll$', 'electricphoenixfll.views.what_is_fll', name='what_is_fll'),
    url(r'^core_values$', 'electricphoenixfll.views.core_values', name='core_values'),
    url(r'^follow_the_phoenix$', 'electricphoenixfll.views.follow_the_phoenix', name='follow_the_phoenix'),
    url(r'^followEnter/$', 'electricphoenixfll.views.followEnter', name='followEnter'),
    url(r'^followList/$', 'electricphoenixfll.views.followList', name='followList'),
    url(r'^about_us$', 'electricphoenixfll.views.about_us', name='about_us'),
    url(r'^calendarDetail/(?P<slug>[\w-]+)/$', 'phoenixEvents.views.calendarDetail', name='pEventsCalendarDetail'),

    url(r'^admin/', include(admin.site.urls)),
)

最佳答案

reverse() 的第二个位置参数是 urlconf 参数:

reverse(viewname[, urlconf=None, args=None, kwargs=None, current_app=None])

要使其正常工作,请使用关键字参数来设置args:

path = reverse('pEventsCalendarDetail', args=(), kwargs={'slug':self.slug})

或者,根本不设置 args:

path = reverse('pEventsCalendarDetail', kwargs={'slug':self.slug})

关于python - 使用 get_absolute_url() 时出现 NoReverseMatch 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23747490/

相关文章:

python - 如何在不使用嵌套 for 循环的情况下将两个列表合并到字典中

python - ftplib,如何在没有 errno 属性的情况下管理异常?

python - 在python文件中使用像 'ë'和 'ç'这样的字符

python - 如何复制嵌套列表中的元素?

python - django-pipeline - 页面加载真的很慢

python - django模型字段依赖于另一个字段的值

django - RESTful 服务参数必须是可发现的吗?

django - Django 1.7迁移以添加原始查询以设置auto_increment初始值

django 1.9 在生产中提供媒体文件

Python ZipFile 模块缓慢提取受密码保护的 zip