我仍在尝试了解url匹配以及reverse()和get_absolute_url()是如何工作的。我想我快到了......
我不太清楚为什么这个模式与我正在逆转的东西不匹配。另外,我不太明白为什么正则表达式(?P \ w +)与带有连字符的slug不匹配。也许这与它为什么不能正常反转有关?它匹配使用。+但不是\ w +。
当我试图扭转以下网址格式时,真正阻止我的是:
url(r'^calendarDetail/(?P<slug>.+)/$', 'phoenixEvents.views.calendarDetail', name='pEventsCalendarDetail'),
我收到此错误:
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'^calendarDetail/(?P<slug>.+)/$', 'phoenixEvents.views.calendarDetail', name='pEventsCalendarDetail'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
答案 0 :(得分:0)
\w
仅涵盖字母数字字符和下划线。连字符不包含在其中。请尝试以下方法:
url(r'^calendarDetail/(?P<slug>[-\w]+)/$', 'phoenixEvents.views.calendarDetail', name='pEventsCalendarDetail'),