使用Mezzanine制作个人BLOG时,进入ADMIN页面会发现只能简单的添加Blog Post, 所有添加的Blog Post都会在Blog这个页面下显示,但实际情况下,我们往往希望将BLOG进行分类,每个分类的Blog在对应的分类目录下显示,那么怎么在Mezzanine中实现这个功能呢,本文将介绍笔者实现的方法。
- Step1 创建分类目录
分类目录是BLOG目录的子目录,因此,在Admin页面中的BLOG下添加一个Link,命名为想要的子目录,比如我的分类目录叫做"Tech",指定其url为"blog/tech"则可以像下图这样:
- Step2 使Blog Post和目录相关联
为Post添加Category即可,如下:
- Step3 点击Tech目录时,在页面上列出Category包含tech的Posts
在Step1中,我们已经指定了Tech目录的url为"blog/tech/",于是我们需要修改urls.py文件,将这个url和我们想要显示的页面对应起来,查阅Mezzanine的文档,关于blog_post_list方法的部分:
mezzanine.blog.views.blog_post_list(request, tag=None, year=None, month=None, username=None, cate
gory=None, template=u'blog/blog_post_list.html', extra_context=None)[source]Display a list of blog posts that are filtered by tag, year, month, author or category. Custom templates are checked for using the name blog/blog_post_list_XXX.html where XXX is either the category slug or author’s username if given.
再查看该方法的源代码:
def blog_post_list(request, tag=None, year=None, month=None, username=None, category=None,template="blog/blog_post_list.html", extra_context=None):
""" Display a list of blog posts that are filtered by tag, year, month, author or category. Custom templates are checked for using the name ``blog/blog_post_list_XXX.html`` where ``XXX`` is either the category slug or author's username if given. """
templates = []
blog_posts = BlogPost.objects.published(for_user=request.user)
if tag is not None:
tag = get_object_or_404(Keyword, slug=tag)
blog_posts = blog_posts.filter(keywords__keyword=tag)
if year is not None:
blog_posts = blog_posts.filter(publish_date__year=year)
if month is not None:
blog_posts = blog_posts.filter(publish_date__month=month)
try:
month = month_name[int(month)]
except IndexError:
raise Http404()
if category is not None:
category = get_object_or_404(BlogCategory, slug=category)
blog_posts = blog_posts.filter(categories=category)
templates.append(u"blog/blog_post_list_%s.html" % str(category.slug))
author = None
if username is not None:
author = get_object_or_404(User, username=username)
blog_posts = blog_posts.filter(user=author) templates.append(u"blog/blog_post_list_%s.html"% username)
prefetch = ("categories", "keywords__keyword")
blog_posts = blog_posts.select_related("user").prefetch_related(*prefetch)
blog_posts = paginate(blog_posts, request.GET.get("page", 1), settings.BLOG_POST_PER_PAGE,settings.MAX_PAGING_LINKS)
context = {"blog_posts": blog_posts, "year": year, "month": month, "tag": tag, "category":category, "author": author}
context.update(extra_context or {}) templates.append(template)
return render(request, templates, context)
可以知道我们可以通过多种方法来筛选Posts(在本例中笔者选择使用Category来筛选),在urls.py中,在"url("^", include("mezzanine.urls")),"这个条目之前添加:
url("^blog/tech/$",blog_post_list,{"category":"tech"}),
将"blog/tech/"定向到blog/blog_post_list_tech.html模板,记住一定要在"url("^", include("mezzanine.urls")),"之前添加,因为urls.py文件也是python脚本,从上到下执行,如果先找到"url("^", include("mezzanine.urls")),"这一条目,则会按照mezzanine.urls中的内容去定向页面,从而认为blog/tech/指向的是一个Blog Post,而我们并不存在一个叫做tech的Blog Post,则会发生404错误。
这样,当我们点击Tech目录时,就可以列出所有category包含tech的Blog Post了。
- Step4 重定义默认的Blog页面
默认情况下,点击Blog目录时会列出所有的Blog Posts,而当我们有了子目录后,并不希望点击BLOG时再列出所有的Blog Post, 这时我们可以自定义想要的Blog页面,比如我想让Blog页面直接显示为一个richtextpage,则我们可以在urls.py中添加一行,然后再在admin页面中编辑他的内容即可,如添加一些对每个分类的描述等:
url("^blog/$",direct_to_template,{"template":"pages/richtextpage.html"}),
注意,也必须在"url("^", include("mezzanine.urls")),"这一条之前添加,否则就会默认显示所有的Blog Post。