Thanks, yes this is *exactly* what I'm after.

I'm coming from other web frameworks where there is more "magic" around 
controllers/views so I was unsure about this solution, but in django it 
appears that a view is explicity just a function that transforms a request 
into a response.

Thanks again.

On Wednesday, July 15, 2015 at 1:23:16 AM UTC+10, ke1g wrote:
>
> You will want a routing view, or a fallback cascade.  In either case, make 
> that urlpattern r'^([\w-]+)$'.  You don't need to escape the - because it's 
> the last char in the class.  You don want to restrict the urls to those in 
> which the entire url matches (^ and $), and the parentheses capture the 
> string as a positional argument to the view.  Your view could then call sub 
> views, something like this:
>
>     def router(request, key):
>         try:
>             return model1_view(request, key)
>         except django.http.Http404:
>             pass
> and as many more try blocks as you need.
>
> Don't put the last sub view in a try lock so that if it's not found 
> anywhere, the 404 takes its natural course.
>
> Each view can use the get_object_or_404() shortcut, or whatever floats 
> your boat.  It's just that they should raise Http404, rather than 
> formatting the 404 response themselves and returning it
>
> The sub views can be class based, so long as they raise Http404 for no 
> such key, or be function views
>
> Doing the outer, routing view, as a class based view is probably less 
> clear (less explicit), and is left as an exercise for the student, if you 
> really want it.
>
> On Tue, Jul 14, 2015 at 9:26 AM, Avraham Serour <[email protected] 
> <javascript:>> wrote:
>
>> What do you mean by flat URL structure?
>>
>> In any case you may have a controller called by the URL dispatcher that 
>> decides which view to use to process the request.
>>
>> No need to complicate on writing you own dispatcher replacement
>>
>> On Tue, Jul 14, 2015, 4:20 PM Mathew Byrne <[email protected] 
>> <javascript:>> wrote:
>>
>>> I have an application that requires a flat URL structure for multiple 
>>> different views.  The single route r"^[\w\-]+" should start by looking at 
>>> slugs for one Model class, and move onto a Category Model class if no match 
>>> is found, then a Vendor model class, and lastly down to the flatpages app.
>>>
>>> I'd like to separate all these models into separate views, but regular 
>>> django urlpatterns wont work here since only the first match is followed.
>>>
>>> What's the best way to implement this requirement?  Is there a way I can 
>>> create my own URL matcher and dispatch to a view based on custom logic?  Is 
>>> there a way to re-dispatch a request after the point at which a match was 
>>> made?
>>>
>>> Thanks,
>>>
>>> Mat
>>>
>>> -- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to [email protected] <javascript:>.
>>> To post to this group, send email to [email protected] 
>>> <javascript:>.
>>> Visit this group at http://groups.google.com/group/django-users.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/django-users/4124aceb-0572-4ee9-ad11-63eb71f71c01%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/django-users/4124aceb-0572-4ee9-ad11-63eb71f71c01%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>  -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/CAFWa6tKunbukyag%3DNkO%3D-E2c61vQErnRqjSgXjnuvO4h1MNgWA%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/django-users/CAFWa6tKunbukyag%3DNkO%3D-E2c61vQErnRqjSgXjnuvO4h1MNgWA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4728ce1f-1576-4463-936e-3755188487bd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to