views:

285

answers:

1

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:

    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'),
+7  A: 

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

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.

Alasdair
I was trying to get around creating my own view, but just wrapping object_list sounds like a reasonable idea
scompt.com
Good answer -- You can do much more with generic views when you take them out of urls.py, and use them inside your own view functions
Ian Clelland
Good link! I had always considered generic views to be something that you just use in urls.py.
scompt.com