Django Macros Url. Routing must be simple!

62 views
Skip to first unread message

Alexandr Shurigin

unread,
Jun 2, 2014, 2:49:37 PM6/2/14
to django...@googlegroups.com
Hi all!

Yestarday i released beta version of new django routing helper component.

Routing must be simple. Now we talking with django core team about making django routing simpler by default.

If anybody is interested about it, you can use right now my component to make your routes simple, clean and readable in future instead of native regular expressions.

http://phpdude.github.io/django-macros-url/

Tom Evans

unread,
Jun 2, 2014, 6:31:21 PM6/2/14
to django...@googlegroups.com
This looks interesting - at first glance I would have dismissed this
(once you know PCRE, why learn another set of rules?) I'm glad I
looked further, because this is not a new lingo; it just provides some
macro magic to save some space, repetition and avoid mistakes, so this
does seem a very useful thing to have.

It's very similar to adding POSIX style regular expression named
character sets (and user defined ones at that) to re, and does
simplify and normalize your rules. I wonder if conceptually and
visually it might work better pretending they are named character
sets, eg:

macrosurl.url('search/[:search_id:]')

Two things:

Sometimes I have two things of the same type, eg two attributes that
are search ids. How would I specify the names of the parameters with a
macro?

Secondly:

> Once Macros Url finished compile regex pattern, it makes normalization of it by rules:
> Strip from left side all whitespace and ^
> Strip from right side of pattern all whitespace and $
> Add to left side ^
> Add to right side $

So why bother include the ^$ in your examples, save an extra couple of
characters!

Cheers

Tom

PS: I know python re is not "PCRE", but it is a good term to describe
the several perl-like RE implementations (Perl, PCRE, Python etc).

Alexandr Shurigin

unread,
Jun 3, 2014, 9:14:58 AM6/3/14
to django...@googlegroups.com
Hi, thank you for review :)

Yes this s just thing to make groups more clean and simple.

used :name as pattern from Ruby On Rails naming. Looks simple and avoid errors.

You can combine base macro types with your names by underscope. For example

/:book_id/:product_id/:id

You will have 3 different matches in your kwargs of course.

Works with any masks and any 'deep'.

/:main_product_id/:main_news_category_id/

Try it!

This component make me happy and i would share it to everyone until django-core team doesn't make ame way out of the box by default.

One small thing - url normalization add $ to end of patterns, this can make problem if you make "include('project.app.urls')" via Macros Url. I know about this, but i just propose use default django url for this. If core team implement this as part of core, they will have context of added view(or include) and can make choose about ending $. Macros url build like a regex preprocessor and have not this context. Anyway includes 99% cases are simple plain text paths and not need macro embedding into it, there can be used django url function without any problems. For me more important to add $ to end of real pattern url because sometimes this can be forgotten :)

Alexandr.

Alexandr Shurigin

unread,
Jun 3, 2014, 9:17:42 AM6/3/14
to django...@googlegroups.com
Just readed about "POSIX style regular expression named character sets". Good idea too. But for now this is utility application with simple syntax, i hope soon django core team think about implementing this as core part and they will choose best naming style i hope :)

I just used ROR naming standart. This looks simple and very clean.


On Tuesday, June 3, 2014 1:49:37 AM UTC+7, Alexandr Shurigin wrote:

Alexandr Shurigin

unread,
Jun 3, 2014, 9:20:32 AM6/3/14
to django...@googlegroups.com
Sorry for my english. Not my primary language :(

I trying to learn it :-)

Alexandr Shurigin

unread,
Jun 3, 2014, 9:46:05 AM6/3/14
to django...@googlegroups.com
I make mistake. I have context of course :)

Added support for include() in view parameter. Now it build (normalize) right patterns without endng dollar sign if include view was passed.

All works automatically and like default django url function.

Version bumped to 0.1.1.


On Tuesday, June 3, 2014 1:49:37 AM UTC+7, Alexandr Shurigin wrote:
Reply all
Reply to author
Forward
0 new messages