Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
django对象标记过滤器_Django_Object_Filter - Fatal编程技术网

django对象标记过滤器

django对象标记过滤器,django,object,filter,Django,Object,Filter,我正在调用html页面上的post.objects.all对象。在最后一篇文章的同一页上。我做了一个过滤器,但我得到一个错误。调用我想做的最后3个帖子 我无法键入.objects.order_by(),因为它可能是一个对象 views.py : def post_index(request): post_list = Post.objects.all() category = Category.objects.all() query = request.GE

我正在调用html页面上的post.objects.all对象。在最后一篇文章的同一页上。我做了一个过滤器,但我得到一个错误。调用我想做的最后3个帖子

我无法键入.objects.order_by(),因为它可能是一个对象

    views.py :
   def post_index(request):
    post_list = Post.objects.all()
    category = Category.objects.all()
    query = request.GET.get('q')
    if query:
        post_list = post_list.filter(
            Q(post_title__icontains=query) |
            Q(post_content__icontains=query) |
            Q(post_date__date=query) 
        ).distinct()

    paginator = Paginator(post_list, 18)  # Show 5 contacts per page

    page = request.GET.get('page')
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        posts = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        posts = paginator.page(paginator.num_pages)
    context = {
        'posts': posts,
        'category':category,
    }
    return render(request, "post/post-list.html", context)
    html:

        {%for post in posts|lasted %}

    filter.py

            @register.filter
        def lasted(post):


            return post.objects.order_by('post_date')[:3]


    AttributeError at /post/index/
    'Page' object has no attribute 'objects'
    Request Method: GET
    Request URL:    http://127.0.0.1:8000/post/index/
    Django Version: 2.2.7
    Exception Type: AttributeError
    Exception Value:    
    'Page' object has no attribute 'objects'
    Exception Location: C:\Users\Barbaros\Desktop\All\env\lib\site-packages\django\template\defaultfilters.py in lasted, line 73
    Python Executable:  C:\Users\Barbaros\Desktop\All\env\Scripts\python.exe
    Python Version: 3.7.4
    Python Path:    
    ['C:\\Users\\Barbaros\\Desktop\\All',
     'C:\\Users\\Barbaros\\Desktop\\All\\env\\Scripts\\python37.zip',
     'C:\\Users\\Barbaros\\Desktop\\All\\env\\DLLs',
     'C:\\Users\\Barbaros\\Desktop\\All\\env\\lib',
     'C:\\Users\\Barbaros\\Desktop\\All\\env\\Scripts',
     'c:\\users\\barbaros\\appdata\\local\\programs\\python\\python37-32\\Lib',
     'c:\\users\\barbaros\\appdata\\local\\programs\\python\\python37-32\\DLLs',
     'C:\\Users\\Barbaros\\Desktop\\All\\env',
     'C:\\Users\\Barbaros\\Desktop\\All\\env\\lib\\site-packages']
    Server time:    Fri, 22 Nov 2019 20:36:41 +0000 
我无法键入.objects.order_by(),因为它可能是一个对象

    views.py :
   def post_index(request):
    post_list = Post.objects.all()
    category = Category.objects.all()
    query = request.GET.get('q')
    if query:
        post_list = post_list.filter(
            Q(post_title__icontains=query) |
            Q(post_content__icontains=query) |
            Q(post_date__date=query) 
        ).distinct()

    paginator = Paginator(post_list, 18)  # Show 5 contacts per page

    page = request.GET.get('page')
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        posts = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        posts = paginator.page(paginator.num_pages)
    context = {
        'posts': posts,
        'category':category,
    }
    return render(request, "post/post-list.html", context)
    html:

        {%for post in posts|lasted %}

    filter.py

            @register.filter
        def lasted(post):


            return post.objects.order_by('post_date')[:3]


    AttributeError at /post/index/
    'Page' object has no attribute 'objects'
    Request Method: GET
    Request URL:    http://127.0.0.1:8000/post/index/
    Django Version: 2.2.7
    Exception Type: AttributeError
    Exception Value:    
    'Page' object has no attribute 'objects'
    Exception Location: C:\Users\Barbaros\Desktop\All\env\lib\site-packages\django\template\defaultfilters.py in lasted, line 73
    Python Executable:  C:\Users\Barbaros\Desktop\All\env\Scripts\python.exe
    Python Version: 3.7.4
    Python Path:    
    ['C:\\Users\\Barbaros\\Desktop\\All',
     'C:\\Users\\Barbaros\\Desktop\\All\\env\\Scripts\\python37.zip',
     'C:\\Users\\Barbaros\\Desktop\\All\\env\\DLLs',
     'C:\\Users\\Barbaros\\Desktop\\All\\env\\lib',
     'C:\\Users\\Barbaros\\Desktop\\All\\env\\Scripts',
     'c:\\users\\barbaros\\appdata\\local\\programs\\python\\python37-32\\Lib',
     'c:\\users\\barbaros\\appdata\\local\\programs\\python\\python37-32\\DLLs',
     'C:\\Users\\Barbaros\\Desktop\\All\\env',
     'C:\\Users\\Barbaros\\Desktop\\All\\env\\lib\\site-packages']
    Server time:    Fri, 22 Nov 2019 20:36:41 +0000 

我以为你在昨天的评论后更改了问题中的代码。现在,请尝试{%forpost in posts.object|list | last%}并按排序(或排序)和键参数(键是comparisison post_date的func)将其在过滤器中排序为列表。或者尝试传递到Paginator排序查询集Paginator(post_list.order_by(“…”),18),而不使用自定义过滤器,然后–ААаСаааа18小时

您的views.py中已经有posts queryset。并且不需要在filter.py中调用
对象
。尝试返回post.order\u by('post\u date')[:3]不要尝试:“Page”对象没有属性“order\u by”,我以为你在yestarday评论后更改了问题中的代码。现在请尝试
{%forpost in posts.object|list | last%}
,并按
排序
(或
排序
)和键参数(键是用于比较
post_日期
的func)对其在过滤器中作为列表排序。或者尝试传递到Paginator排序查询集
Paginator(post_list.order_by(“…”),18)
,而不使用自定义过滤器