Saturday, June 3, 2017

Django URL dispacher

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. 

--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5000b823-b3e3-4a98-aaaa-21d545610227%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment