我有简单的“帖子”模型,代表我的博客条目:
class Post(models.Model):
title = models.CharField('title', max_length=200)
slug = models.SlugField('slug', unique_for_date='creation_time')
creation_time = models.DateTimeField('creation time', auto_now_add=True)
content = models.TextField('content')
@permalink
def get_absolute_url(self):
return ('devblog_post_url', (), {
'year': self.creation_time.year,
'month': self.creation_time.month,
'day': self.creation_time.day,
'slug': self.slug})
在我的博客索引中,我想用这个视图对这些帖子进行分页:
def index_view(request):
published_posts = Post.objects.all()
paginator = Paginator(published_posts, 10)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
post_list = paginator.page(page)
except (EmptyPage, InvalidPage):
post_list = paginator.page(paginator.num_pages)
return render_to_response('devblog_index.html', {"post_list": post_list})
现在问题是,一旦我使用devblog_index.html
模板为帖子调用get_absolute_url方法,我会收到空白网址:
{% for post in post_list.object_list %}
<a href="/{{ post.get_absolute_url }}/">{{ post.title }}</a><br />
{% endfor %}
修改
以下是我的应用程序的urls.py:
来自django.conf.urls.defaults import *
urlpatterns = patterns('',
url(r'^$',
view='devblog.views.index_view',
name='devblog_index'
),
url(r'^(?P<year>\d{4})/(?P<month>\w{1,2)/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$',
view='devblog.views.post_view', name='devblog_post_url'
)
)
在主urls.py上,我简单地将其包含为(r'^blog/', include('devblog.urls'))
此空白字符串网址的问题可能在哪里?
此致
答案 0 :(得分:5)
将(?P<month>\w{1,2)
更改为(?P<month>\d{1,2})
。