# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import Http404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views.generic import ListView, DetailView
from blog.models import Post,Tag,Category
from config.models import SideBar
from comment.models import Comment
# Create your views here.
class CommonMixin(object):
def get_context_data(self):
nav_cates = Category.objects.filter(status=1).filter(is_nav=True)
cates = Category.objects.filter(status=1).filter(is_nav=False)
side_bars = SideBar.objects.filter(status=1)
recently_posts = Post.objects.filter(status=1)[:10]
recently_comments = Comment.objects.filter(status=1)[:10]
extra_context = {
'nav_cates': nav_cates,
'cates': cates,
'side_bars': side_bars,
'recently_comments': recently_comments,
'recently_posts': recently_posts,
}
return super(CommonMixin,self).get_context_data(**extra_context)
class BasePostsView(ListView,CommonMixin):
model = Post
template_name = 'blog/list.html'
context_object_name = 'posts'
paginate_by = 1 # 分页,pagesize
allow_empty = False # 当url输入的页码没有的时候自动重定向到最后一页
class IndexView(BasePostsView):
pass
class CategoryView(BasePostsView):
def get_queryset(self):
qs = super(CategoryView, self).get_queryset()
cate_id = self.kwargs.get('category_id')
qs = qs.filter(category_id=cate_id)
return qs
class TagView(BasePostsView):
def get_queryset(self):
tag_id = self.kwargs.get('tag_id')
try:
tag = Tag.objects.get(id=tag_id)
except Tag.DoesNotExist:
return []
posts = tag.posts.all()
return posts
class PostView(DetailView,CommonMixin):
model = Post
template_name = 'blog/detail.html'
context_object_name = 'post'
上面是view的代码,我现在看到第7章,之前用function view的时候,通用数据是能显示出来的,现在用class based view,通用数据无法显示出来 下面是html list.html
{% extends "blog/base.html"%}
{% block content %}
<ul>
{% if posts %}
<ul>
{% for post in page_obj %}
<li>
title : <a href="{% url 'detail' post.id %}"> {{ post.title }} </a><br/>
desc: {{ post.desc}}
</li>
{% endfor %}
{% if page_obj.has_previous %} <a href="?page={{ page_obj.number | add:"-1" }}">上一页</a> {% endif %}
Page {{ page_obj.number }} of {{ paginator.num_pages }}.
{% if page_obj.has_next %} <a href="?page={{ page_obj.number | add:"1" }}">下一页</a> {% endif %}
</ul>
{% else %}
Empty.
{% endif %}
</ul>
{% endblock %}
base.html
<h1>typeidea blog</h1>
<ul>
{% for cate in nav_cates %}
<li><a href="{% url 'category' cate.id %}">{{ cate.name }}</a></li>
{% endfor %}
</ul>
<hr/>
<div id="content">
{% block content %}
{% endblock %}
</div>
<hr/>
<ul>
{% for cate in cates %}
<li><a href="{% url 'category' cate.id %}">{{ cate.name }}</a> </li>
{% endfor %}
</ul>
<ul>
{% for side in side_bars %}
<h4>{{ side.title }}</h4>
<div>
{% if side.display_type == 1 %}
{% autoescape off %}
{{ side.content }}
{% endautoescape %}
{% elif side.display_type == 2 %}
<ul>
{% for post in recently_posts %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
{% elif side.display_type == 4 %}
{% for comment in recently_comments %}
<li>{{ comment.content }}</li>
{% endfor %}
{% endif %}
</div>
{% endfor %}
</ul>
detail.html
{% extends "blog/base.html"%}
{% block content %}
<h2>{{ post.title }}</h2>
<div>
<ul>
<li><a href="/">首页</a> </li>
<li><a href="/">{{ post.category.name}}</a> </li>
<li>正文</li>
</ul>
</div>
{% endblock %}