繁体   English   中英

使用get_absolute_url()的NoReverseMatch错误

[英]NoReverseMatch error using get_absolute_url()

我正在尝试使用get_absolute_url遵循DRY规则。 如果我编码该类以直接从子弹构建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})

不要同时使用permalink装饰器 reverse()调用。 他们俩都做同样的事情。 放下装饰器:不推荐使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM