所以我有以下应用程序,它应该类似于一个多用户博客,只需为每个作者提供视图。
除了帖子的绝对网址外,一切正常。(我想保持非常简单 - 无需使用发布/草稿等)
这是我的代码:
模型.py:http : //pastebin.com/pT91DUdZ
@permalink
def get_absolute_url(self):
return ('blog_detail', None, {
'year': self.created.year,
'month': self.created.strftime('%b').lower(),
'day': self.created.day,
'slug': self.slug
})
view.py:http://pastebin.com/FTb0s97u _
def post_list(request, page=0, paginate_by=20, username=None, **kwargs):
author = None
if username is not None:
author = get_object_or_404(User, username=username)
return list_detail.object_list(
request,
queryset=author.post_set.created(),
extra_context = {'author' : author},
paginate_by=paginate_by,
page=page,
**kwargs
)
urls.py:http://pastebin.com/Nv2SRbhe _
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('userblog.views',
url(r'^(?P<username>.*)/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$',
view='post_detail',
name='blog_detail'
),
url(r'^(?P<username>.*)/(?P<page>\d+)/$',
view='post_list',
name='blog_index_paginated'
),
url(r'^(?P<username>.*)/$',
view='post_list',
name='blog_index'
),
谢谢参观 :)
编辑:我现在做了以下事情。我知道这很丑陋,但我不知道如何正确地做到这一点!
def get_absolute_url(self):
return '/blogs/%s/%s/%s/%s/%s/' % (self.author.username, self.created.year, self.created.strftime('%b').lower(), self.created.day, self.slug)