前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >django 通用视图(generic view)获取 request

django 通用视图(generic view)获取 request

作者头像
卓越笔记
发布2023-02-17 14:52:14
2950
发布2023-02-17 14:52:14
举报
文章被收录于专栏:卓越笔记
代码语言:javascript
复制
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)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-10-8 1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档