On Fri, 2006-07-21 at 04:53 -0700, Tomas Jacobsen wrote:
> I have done some more reading of other tutorials, and Im slowly getting
> further. I now understand that I can use django generic views instead
> of writing my own view. (sorry Malcolm, I diden't get that before). I
> have now made one url pattern for my project listing. It's listing all
> of my project, regardless of the category their in, just the way I
> want.

Rock and roll! Glad you're making progress. :-)

> 
> But I don't understand it good enough to write more url patterns. I
> don't know how to write a pattern for listing projects inside a
> spesific category. And I don't know how to make a pattern for the
> detail page.
> 
> For my portfolio/models.py I use this code : http://phpfi.com/133790
> 
> 
> I use this pattern now for listing all my projects:
> 
> info_dict = { 'queryset': Project.objects.all(), }
> 
> urlpatterns = patterns('',
> 
>       (r'^portfolio/$', 'django.views.generic.list_detail.object_list',
> dict(info_dict, template_name="portfolio/projects_list.html")),
> )
> 
> I have tried adding this line:
> 
> (r'^portfolio/(?P<category_slug>[-\w]+)/$',
> 'django.views.generic.date_based.object_detail', dict(info_dict,
> slug_field='slug',
> template_name="portfolio/category_spesific_list.html")),
> 
> But I get the error" object_detail() got an unexpected keyword argument
> 'category_slug' " when I try mydomain.com/portfolio/3d
> 
> Im not shure how that would line would work for only listing project in
> the given category anyway.

OK, now for some good news/bad news information: you can't get exactly
the effect you want with just generic views. This is because you are
wanting to do some computation based on the information in the URL and
then pass the results to the view function (for a start, your queryset
is dependent upon the URL). That's the bad news.

The good news is that it's about two lines of extra code to work out the
information you need and then you can pass the bulk of the work off to
generic views.

What you can do is write your own view function (yeah, you just got away
from your own views and now you have to go back... life's like that
sometimes) that accepts the category name as a parameter. This function
will work out the correct query set and pass that information (along
with the category name and whatever else you want) to the generic view
to extract the information and call your template.

Here's what your view might look like:

        def category_view(request, cat_name):
            queryset = Project.objects.filter(category__name = cat_name)
            context = {'category_name': cat_name}
            return object_detail(request, queryset, extra_context =
        context)
        
Note that there are two underscores between "category" and "name" in the
filter. That syntax is explained in [1]. Once you have worked out the
query set, you pass that and the name (via extra_context) and the
original request object off to the generic view to work out the result.
I guess in your case you probably want to pass template_name into that
call to object_detail as well, but at that point it's all just a normal
function call, so you can look at the parameters the object_detail()
function takes (see [2]) and pass in whatever you like.

I wrote about this elsewhere ([3]) a little bit as well, although it
pretty much just says exactly what I wrote here, I suspect.

[1]
http://www.djangoproject.com/documentation/db_api/#lookups-that-span-relationships

[2]
http://www.djangoproject.com/documentation/generic_views/#django-views-generic-list-detail-object-detail

[3]
http://www.pointy-stick.com/blog/2006/06/29/django-tips-extending-generic-views/

Best wishes,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to