【问题标题】:NoReverseMatch error using get_absolute_url()使用 get_absolute_url() 的 NoReverseMatch 错误
【发布时间】:2014-07-07 23:43:27
【问题描述】:

我正在尝试使用 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)),
)

【问题讨论】:

    标签: python django python-2.7 django-urls


    【解决方案1】:

    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})
    

    【讨论】:

    • 谢谢!!我认为该名称提供了对 urlconf 信息的引用。这不是我在 urls.py 文件中指定的内容吗?不会将其添加到对 reverse() 的调用中,将 url 嵌入到代码中的另一个位置......这只会让我失去 url 的“域”部分。我应该从 os.path 获得它还是有更多 Django 风格的方法来获得它?
    • @Arana 有帮助吗? Django 会自动找到您的 urls.py 文件,无需手动指定 urlconf 参数。除非你在“跳出框框”做某事。
    • 是的,确实如此!抱歉,我不是很清楚。我现在缺少域(Test 1)添加最后一部分的最佳方法是什么?跨度>
    • 我知道它会自动找到我的 urls.py,这就是为什么我质疑 reverse() 中指定 urlconf 的参数的“需要”。它是旧版本的遗留物,还是我想使用它的另一个原因? (只是想了解更多...)
    • @Arana 好的,谢谢,看看stackoverflow.com/questions/2345708/…
    【解决方案2】:

    不要同时使用permalink 装饰器 reverse() 调用。他们都做同样的事情。删除装饰器:它已被弃用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-22
      • 2011-06-26
      • 2016-12-14
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多