我可以在使用Django通用视图时过滤request.user吗?

时间:2021-03-04 19:16:33

I want to do something like this (from my urls.py), but I don't know if it's possible to get the user making the request:

我想做这样的事情(来自我的urls.py),但我不知道是否有可能让用户提出请求:

    url(r'^jobs/(page(?P<page>[0-9]+)/)?$',
        object_list, {'queryset': Job.objects.filter(user=request.user), 
                      'template_name': 'shootmpi/molecule_list.html'},
        name='user_jobs'),

1 个解决方案

#1


8  

You can write a wrapper function that calls object_list with the required queryset.

您可以编写一个使用所需查询集调用object_list的包装函数。

In urls.py:

url(r'^(page(?P<page>[0-9]+)/)?$', 'views.user_jobs', name='user_jobs')

In views.py:

from django.views.generic.list_detail import object_list

def user_jobs(request, page):
    job_list=Job.objects.filter(user=request.user)
    return object_list(request, queryset=job_list,
        template_name='shootmpi/molecule_list.html',
        page=page)

There's a good blog post by James Bennett on using this technique.

詹姆斯贝内特有一篇关于使用这种技术的好文章。

#1


8  

You can write a wrapper function that calls object_list with the required queryset.

您可以编写一个使用所需查询集调用object_list的包装函数。

In urls.py:

url(r'^(page(?P<page>[0-9]+)/)?$', 'views.user_jobs', name='user_jobs')

In views.py:

from django.views.generic.list_detail import object_list

def user_jobs(request, page):
    job_list=Job.objects.filter(user=request.user)
    return object_list(request, queryset=job_list,
        template_name='shootmpi/molecule_list.html',
        page=page)

There's a good blog post by James Bennett on using this technique.

詹姆斯贝内特有一篇关于使用这种技术的好文章。