from django.views import generic
from blog.models import *
from ipware.ip import get_ip
class IndexView(generic.ListView):
template_name = 'lw-index-noslider.html' # 加载该html文件
context_object_name = "articles" # 是数据库搜索出来的结果存放的变量名字,用于模板循环显示
paginate_by = 4 # 设置分页中每一页的记录数目
model = Article # 定义从哪份model中查询
def get_queryset(self):
return Article.objects.filter(show_status=True).order_by('-time_created')
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['cloudtags'] = Tag.objects.filter().order_by("-id")[:8]
context['nodes'] = Node.objects.filter().order_by("-id")[:8]
context['quotations'] = get_quotations(2)
return context
def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
# 这里的 request 就是普通 view 里面 request
print(get_ip(request))
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
return handler(request, *args, **kwargs)