增加文章url
url(r'^article/(?P<id>\d+)/$', articleView, name='article'),
增加视图函数articleView
def articleView(request, id):
try:
article = Article.objects.get(pk=id)
except Article.DoesNotExist:
return render(request, 'failure.html', {'msg':'article not found!'})
return render(request, 'article.html', locals())
模板中增加链接
<h3><a href="{% url 'article' article.pk %}">{{ article.title }}</a></h3>
*自定义filter
blog下新建templatetags包,注意是包
包内新建 myFilter.py
from django import template
register = template.Library()
@register.filter
def month_to_upper(key):
ms = [str(i + 1) + '月' for i in range(12)]
return ms[key.month - 1]
模板使用自定义filter, 使用前要先加载文件
{% load myFilter %}
{{ article.date_published | month_to_upper }}