Wednesday, November 30, 2011

Re: A view associated with a lot of urls


I don't quite understand what you mean here. You want me to create a
CategoryModel of which all my Category models should be derived ? If
that's it, how can it help me to determine if URL is valid or not, I
can't see how that works.

Sorry, it was DrBloodmoney, who wrote this:

class Category(models.Model):
   name = models.CharField.... # should be unique

class Item(models.Model):
 name = models.CharField()
  category = models.ForeignKey(Category, related_name='items')

in your view you do so:

def view(request, category, item):
     items= get_object_or_404(Category,name=category).items().filter(name=item)
     ....

That's it.

Now if someone enters a category by hand which does not exist, he gets a 404. If the category exists, but there is no item with such a name items will be empty.

Bye
Ivo

No comments:

Post a Comment