On Jun 17, 7:39 am, "C. Feldmann" <casper.feldm...@googlemail.com> wrote: > Good morning, > > I was wondering how to implement the following without having to > hardcode each url. > I have different apps like i.e. books, authors, magazines, etc. > Normally I would call upon default views by using something > like:http://localhost/books/James_and_the_Giant_Peach > orhttp://localhost/authors/Roald_Dahl > > How can I configure django to search through the different > possibilities if someone i.e. called up the > urlhttp://localhost/James_and_the_Giant_Peach > orhttp://localhost/Roald_Dahl? > > I could probably hardcode "redirects" in the urls file, but there are > too many possible urls and they change too often. How can I make this > more dynamic? > > Thanks > Casper
I'm not entirely sure I understand what you need to do. But it sounds like you want a 'dispatcher' method which catches all URLs, checks through all the things they could be, then hands off to the correct view. It's not clear whether you want to actually serve the page from the initial URL - /Roald_Dahl/ - or force the browser to redirect to the actual URL, /authors/Roald_Dahl/. I would recommend the second (for SEO reasons it's always best to serve content on a single canonical URL) but it's up to you. from myapp.models import Books, Authors from myotherapp.models import Whatever def dispatcher(request, slug): models = (('books': Books), ('authors': Authors), ('whatever': Whatever)) for name, model in models: try: obj = model.objects.get(slug=slug) return HttpResponseRedirect(reverse(name, slug)) except Model.DoesNotExist: pass # no matching object found, return 404 return HttpResponseNotFound() -- DR. --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---