views:

215

answers:

1

Is it possible to use a generic view with additional parameters in the URL mapping - i.e. I got the following model:

class Route(models.Model):
    area = models.ForeignKey(Area)
    slug = models.SlugField(null=True,blank=True)

    @models.permalink
    def get_absolute_url(self):
        return ('route_details', (), {'area': self.area.slug, 
                                      'slug': self.slug})

This URL-Mapping:

url(r'^(?P<area>[-\w]+)/(?P<slug>[-\w]+)/$','routes.views.route_details',
    name='route_details')

And this trivial view:

def route_details(self, area, slug):
    route = get_object_or_404(Route, slug=slug)
    return render_to_response("routes/route_details.html", {'route': route})

As you can see, I'm actually identifying the route just by the route's slug and the area slug is just to shape the url (e.g. routes/central-park/rat-rock). Can I do the same just using a generic view?

+1  A: 

Sure, something like this:

url(r'^(?P<area>[-\w]+)/(?P<slug>[-\w]+)/$',
    'django.views.generic.list_detail.object_detail',
    {'queryset': Route.objects.all()}
    name='route_details')

Should just work.

Be sure to either set template_object_name to "route" or use "object" in template.

Dmitry Shevchenko
Tried that - "object_detail() got an unexpected keyword argument 'area'"
cvk