Skip to content

Commit

Permalink
blog - add category and filter
Browse files Browse the repository at this point in the history
  • Loading branch information
liulixiang1988 committed May 28, 2014
1 parent 5b0ff1d commit ad63970
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions django/mydjangosite/blog/models.py
Expand Up @@ -18,10 +18,10 @@ def __str__(self):

class Post(models.Model):
title = models.CharField('标题', max_length=120)
author = models.ForeignKey(User)
author = models.ForeignKey(User, verbose_name='作者')
content = models.TextField('内容')
po_type = models.ForeignKey(Category, verbose_name='文章分类', blank=True, null=True)
pub_date = models.DateTimeField(auto_now_add=True)
pub_date = models.DateTimeField('发布日期', auto_now_add=True)

class Meta:
verbose_name = '文章'
Expand Down
2 changes: 1 addition & 1 deletion django/mydjangosite/blog/templates/blog/_side.html
Expand Up @@ -11,7 +11,7 @@ <h3>文章分类</h3>
{% if categories %}
<ul class="list-unstyled">
{% for c in categories %}
<li>- {{ c.name | safe }}</li>
<li>- <a href="{% url 'blog:category' c.pk %}">{{ c.name | safe }}</a></li>
{% endfor %}
</ul>
{% endif %}
Expand Down
6 changes: 6 additions & 0 deletions django/mydjangosite/blog/templates/blog/index.html
Expand Up @@ -3,6 +3,12 @@
{% block title %}首页-{% endblock %}

{% block content %}
{% if is_category %}
<div class="well well-sm">
"{{ cat_name | safe }}"分类中共有<span class="label label-info">{{ posts | length }}</span>
</div>
{% endif %}

{% for post in posts %}
{% include 'blog/_post.html' %}
{% endfor %}
Expand Down
1 change: 1 addition & 0 deletions django/mydjangosite/blog/urls.py
Expand Up @@ -7,4 +7,5 @@
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^post/(?P<pk>\d+)/$', views.post, name='post'),
url(r'^category/(?P<pk>\d+)/$', views.category, name='category')
)
8 changes: 8 additions & 0 deletions django/mydjangosite/blog/views.py
Expand Up @@ -16,3 +16,11 @@ def post(request, pk):
categories = Category.objects.all()
return render(request, 'blog/post.html', {'post': post, 'categories': categories})


def category(request, pk):
"""文章分类"""
cat = get_object_or_404(Category, pk=pk)
posts = cat.post_set.all()
return render(request, 'blog/index.html', {'posts': posts, 'categories': Category.objects.all(),
'is_category': True, 'cat_name': cat.name})

Binary file modified django/mydjangosite/djangotutorial
Binary file not shown.

0 comments on commit ad63970

Please sign in to comment.