Django URL dispacher

30 views
Skip to first unread message

Domagoj Kovač

unread,
Jun 3, 2017, 4:57:54 PM6/3/17
to Django users
Hi Everybody,

I have a question. Lets say i have two routes routes like this:

url(r'^(?P<article_category_slug>[\w-]+)/$', views.view_article_category, name="article-category"),
url(r'^(?P<product_category_slug>[\w-]+)/$', views.view_product_category, name="product-category"),

Lets say that i have a article_category called news and product category call books.

When user requests for /news - first route is matched, request is successfully resolved and that's ok. 
When user requests for /books - first route is matched but books doesn't exist in article_category table and some kind of error is raised.

Is there any way to do something like this, when request is not resolved on first matched url (like in the second example), continue matching until request is successfully resolved. 

i have two solution but non of those solution i don't like.

1. just add products prefix to product-category url
2. use only one route in urlconfig:
url(r'^(?P<category_slug>[\w-]+)/$', views.view_category_resolver, name="category"),
and then in my view i would implement some kind of routing logic - i would say this is better solution for me because i would like to have my urls as simple as possible. 

But, is there any way to do it like i described in the beginning of my post?

I have one more good example for this situation.

Generally when i construct my cms i like to separate articles to "static" and "categorizes". Static articles are those article that don't have category related to them, for example "about us", "general terms" and so on. Categorized are the ones that belong to certain category, for example "U2 concert in London" belongs to news category.

So lets say that i have article about-us and article-category news. I would like to:
1. on /about-us route - display about us page
2. on /news route - display news list page

Here i have the same problem. 
Message has been deleted

Melvyn Sopacua

unread,
Jun 3, 2017, 6:13:39 PM6/3/17
to django...@googlegroups.com

On Saturday 03 June 2017 13:57:54 Domagoj KovaÄ wrote:

 

> url(r'^(?P<article_category_slug>[\w-]+)/$',

> views.view_article_category, name="article-category"),

>

> url(r'^(?P<product_category_slug>[\w-]+)/$',

> views.view_product_category, name="product-category"),

>

>

> Lets say that i have a article_category called news and product

> category call books.

>

> When user requests for /news - first route is matched, request is

> successfully resolved and that's ok.

> When user requests for /books - first route is matched but books

> doesn't exist in article_category table and some kind of error is

> raised.

>

> Is there any way to do something like this, when request is not

> resolved on first matched url (like in the second example), continue

> matching until request is successfully resolved.

>

> i have two solution but non of those solution i don't like.

>

> 1. just add products prefix to product-category url

 

And what's not to like? It's clear to humans and has SEO value.

 

> 2. use only one route in urlconfig:

>

> url(r'^(?P<category_slug>[\w-]+)/$', views.view_category_resolver,

> name="category"),

>

> and then in my view i would implement some kind of routing logic - i

> would say this is better solution for me because i would like to have

> my urls as simple as possible.

 

So an article titled 'coffee' and a product category 'coffee' end up with the same URL. And don't think it won't happen. I've seen it many times, especially with products that don't have a plural, like "fish". The theory being that if you name categories plular and products singular, you won't clash.

 

But yes, you would need to do this in a view or .... create your own resolver (see django.urls.resolvers).

 

Also think about the fact that just to resolve your URL, you would need at minimum n database queries, where n is the number of models matching. That don't scale well.

--

Melvyn Sopacua

Domagoj Kovač

unread,
Jun 3, 2017, 6:22:52 PM6/3/17
to Django users
Yes, you are right. Sometimes i get the idea in my head that after some thought actually does not make sense :). I would just add product prefix, this is a good solution. 
Reply all
Reply to author
Forward
0 new messages