Make url patterns group kwargs more simple

371 views
Skip to first unread message

Alexandr Shurigin

unread,
May 28, 2014, 11:56:04 AM5/28/14
to django-d...@googlegroups.com
Hi all.

What do you think about adding some extra default and more simpler syntax for url patterns?

Now patterns looks little bit difficult, especially if you have long urls with 2-3 params.

For example take a look into url.

url(r'^(?P<slug_genre>[^/]+)/(?P<slug>[^/]+)/news/(?P<slug_item>[^/]+)$', views.GameNewsItem.as_view(), name="view_news_view")


This is ‘good seo url’ for big project. This url have game genre, game slug name, /news/ and news slug name.

For example this matches path /arcade/pacman/news/new-update-2014/

This is pretty cool when site have such urls and support all url subpaths, user can simple remove some path in url and go other pages of site catalog structure.

In presented example i wanted to show you this url entry shard to read (but not so difficult to write) when you supporting your project.

When you have about 20-30 same style urls in one file, this makes your urls file unreadable :)

Maybe allow user enter url masks in simpler way by adding some magic?

For django is not problem make url regexps from string with ‘meta tags’ and process it way like it work now. But i think user will be really happy to enter simpler (and shorter!) urls.

I mean we allow user only describe his urls simpler.

For example in ruby on rails user can enter urls like

get 'products/:id/purchase' => 'catalog#purchase', as: :purchase


Where :id is just shortcut for regexp pattern group.

I think we (or me, no difference) can do same way.

We can do it 2 ways:

1. hardcode some usually used tags (for example id, day, year, slug, any other) and words built from them (for example we understand that :id and :product_id have the same path type ([0-9]+)).
2. Make library for registering such tags like template tag library and add rules from #1 to it. Allowing way for users to extend library tags with their own.

Using this way, we can get very simple and fancy urls. For example my game news url will look like:

url(r’^:slug_genre/:slug/news/:slug_item$', views.GameNewsItem.as_view(), name="view_news_view")


This will make urls very short, fast-readable and writable.

Also we can skip in pattern masks ?P, for example from url pattern

(<slug_genre>[^/]+)

we can simple compile 
(?P<slug_genre>[^/]+)

I think a lot of users will appreciate this feature. What you think?

Django urls are not simple right now for users and looks little ugly, because 99% users reuse standard types of masks (id like - [0-9]+, slug like - [a-z0-9]+, year [0-9]{4}, month [0-9]{2}, day [0-9]{2}).

But of course user can combine url shortcuts with their custom regexps.



-- 
Alexandr Shurigin

Shai Berger

unread,
May 28, 2014, 12:53:53 PM5/28/14
to django-d...@googlegroups.com
Hi Alexendr,

On Wednesday 28 May 2014 18:54:05 Alexandr Shurigin wrote:
> Hi all.
>
> What do you think about adding some extra default and more simpler syntax
> for url patterns?
>
[looking for a way to re-write...]
>
> url(r'^(?P<slug_genre>[^/]+)/(?P<slug>[^/]+)/news/(?P<slug_item>[^/]+)$',...
>
[...as...]
>
>
> url(r’^:slug_genre/:slug/news/:slug_item$', ,,,
>
> This will make urls very short, fast-readable and writable.
>
I agree -- but there are two points:

1) The kind of expressions you'd want to use is probably very project-specific;

2) Something very close is easy to achieve, without any change to Django. For
example, for the case you gave above, add this near the top of your urls.py:

import re
def t(tag_url):
"""
Take a URL pattern with parts marked as :tag, and return
the appropriate Django url-pattern
"""
tag = re.compile(r':([\w]+)')
pat, _ = tag.subn(r'(?P<\1>[^/]+)', tag_url)
return pat

and now, you can write your urls as

url(t(r’^:slug_genre/:slug/news/:slug_item$'), ,,,

or even add further:

def turl(pat, *args, **kw): return url(t(pat), *args, **kw)

and write urls as

turl(r’^:slug_genre/:slug/news/:slug_item$', ,,,

Thus, I'm not sure anything needs to be changed in Django to support this.

You can, of course, make some generalization of this and offer it to the
community -- if it becomes very popular, it then may be a good candidate for
inclusion in Django. Just to be clear, I'm *not* saying that it won't be --
just that, before adding something like this to Django, we'd want to see how
it gets used, so the feature we finally implement is useful for many people.

HTH,
Shai.

Sean Bleier

unread,
May 28, 2014, 1:01:44 PM5/28/14
to django-d...@googlegroups.com
You might want to check out Surlex[1], written by Cody Soyland.  It basically does what you want.



--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/etPan.5386069d.57e4ccaf.406%40MacBook-Pro-dude.local.
For more options, visit https://groups.google.com/d/optout.

Alexandr Shurigin

unread,
May 28, 2014, 1:05:29 PM5/28/14
to django-d...@googlegroups.com
Thank you for your answer.

yes, that’ is not big problem in coding this feature locally or in django core. i just wanted to show interesting feature for django users. This feature can help new users with urls patterning and make existing code more readable.

Yes i will make component for django with supporting this feature and will try to publish it anywhere available to community. Maybe people will like it :)

For me this feature looks very usable.

Sorry for my english. Not native language.

-- 
Alexandr Shurigin
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.

Alexandr Shurigin

unread,
May 28, 2014, 1:18:03 PM5/28/14
to django-d...@googlegroups.com
Thank you!

Just checked it, looks really what i mean.

Will check this library more deep.

First comment from developers site http://codysoyland.com/2009/sep/6/introduction-surlex/

>> This is pretty cool. You should build this as a drop-in into django!

:-)

Write clean regular expressions i think is little bit hard for some part of django users, because regexp is not simple for some developers. If allow users use url predefined patterns out of the box users will appreciate it  i think.



-- 
Alexandr Shurigin

From: Bleier Sean sebl...@gmail.com
Reply: django-d...@googlegroups.com django-d...@googlegroups.com
Date: 29 мая 2014 г. at 0:01:43
To: django-d...@googlegroups.com django-d...@googlegroups.com
Subject:  Re: Make url patterns group kwargs more simple

Tim Graham

unread,
May 28, 2014, 1:18:52 PM5/28/14
to django-d...@googlegroups.com
There was also a mention in IRC by a core dev (Jannis, I think) about the possibility of merging http://amitu.com/smarturls/ into core.  I agree URL regexes is something that we could improve, but as there are many solutions out there, this would require discussion and a consensus.

Alexandr Shurigin

unread,
May 28, 2014, 1:23:21 PM5/28/14
to django-d...@googlegroups.com
surlex library looks little bit simpler then smarturls.


    surl('/year/<int4:year>/', 'year.view'),
    surl('/year/<int4:year>/<word:month>/', 'month.view’),
And surlex example
/articles/<year:Y>/<slug:s>/(<page:#>/)
Surlex looks more user-friendly.
I think need to have a look for such libraries, make choose about patterns formatting and implement this right way into django.
Ruby rails way looks simplest of this two. Also we can check ror routing patterns and make choose.
What you think?
alex.
-- 
Alexandr Shurigin

Marc Tamlyn

unread,
May 28, 2014, 3:51:42 PM5/28/14
to django-d...@googlegroups.com

I'm in favour of introducing another URL resolver, as a simpler option not as a replacement. My motivation is not "because it's nicer", rather to lower the boundary to entry. The Regex syntax is difficult for beginners.

There are many competing options, those mentioned and also hurl (github.com/oinopion/hurl). There are probably more around.

At the moment, I'm unsure about which format is preferable, and I'd be more interested to see changes to Django to make it easier to change the URL resolving system, including reversal. Having a standards API documented which allows patterns/URL/resolve to be intermingled with analogues with different rules is a good idea. It's not quite a 'urlbackend' as I'd like different ones to work in the same project, but that's the idea. This would also make handling complex URL resolving concepts easier to do outside of Django (e.g. a ContinueResolving exception views can raise to try later patterns)

I'm not saying any of this is not currently possible - it is. But I'd prefer to introduce a stable, robust API and then look at exact implementations of format.

Marc

Jacob Kaplan-Moss

unread,
May 28, 2014, 3:56:58 PM5/28/14
to django-developers
On Wed, May 28, 2014 at 2:51 PM, Marc Tamlyn <marc....@gmail.com> wrote:

I'm not saying any of this is not currently possible - it is. But I'd prefer to introduce a stable, robust API and then look at exact implementations of format.

I completely agree -- rather than pick something from a bunch of good options, I'd rather us introduce an API to make resolution pluggable and customizable (and then, maybe, choose a good default that's easier than regexes).

I've done some looking into this. It's possible, but going to be pretty difficult: resolution is one of the last great superglobals hanging around Django, and it's very tightly coupled into the core http/wsgi server. It'd take quite a bit of refactoring and cleaning to make this happen, unfortunately. I'm all for it, but it's not going to be easy.

Jacob

Alexandr Shurigin

unread,
May 28, 2014, 4:02:43 PM5/28/14
to django-d...@googlegroups.com
How many functions for now url api requires?

I know about resolve view by path and reverse url to path by namespace, name and params.

I forgot something else?


-- 
Alexandr Shurigin

From: Kaplan-Moss Jacob ja...@jacobian.org
Reply: django-d...@googlegroups.com django-d...@googlegroups.com
Date: 29 мая 2014 г. at 2:56:57
To: django-developers django-d...@googlegroups.com
Subject:  Re: Make url patterns group kwargs more simple

--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.

Alexandr Shurigin

unread,
May 28, 2014, 4:07:33 PM5/28/14
to django-d...@googlegroups.com
happy Urls doesn’t look simpler, just splitter configuration to parts .. This doesn’t make urls more readable i think. Sometimes this way is good, but not always.


-- 
Alexandr Shurigin

Alexandr Shurigin

unread,
May 31, 2014, 3:44:33 PM5/31/14
to django-d...@googlegroups.com
hi all.

I developed first version of application, you can take a look into it.


You can take a look into tests to see for really big url patterns :))

Library is simple as possible and makes a lot of extra work.

I think some type of library must be in core, what you think?

Thanks

-- 
Alexandr Shurigin

Moayad Mardini

unread,
Jun 2, 2014, 2:38:11 AM6/2/14
to django-d...@googlegroups.com
Hi Alexandr,

Thanks for the nice suggestion. It's good to try to improve Django in an area you think it might be lacking.

I also think this is a really nice feature to add to the framework, which will make it more beginner-friendly.

I'll try to discuss what I think is the most important features that should be considered in a potential new resolver that might be added to the core. There are currently many options to start with, including:

The features include:

1) The syntax of the new URL:
Since the whole point of introducing another URL resolver is to make the syntax simpler, I think this is the most important feature.
The options are:
A) The colon syntax: url('news/:year/:month/:day$', 'news_view')
B) An angle brackets tag syntax: surl('news/<year>/<month>/<day>', 'news_view')
C) A chain of function calls: str(repeats(literal('a'),2,3))

IMHO, option C is too ugly, but option B is the most natural for a beginner to use, especially when used with a named group, see point 2.

2) How expressive the patterns names are:
A) Expressive, reflects what the real regex will be: <int4:birth_year>
B) The predefined pattern uses a simple name :  <year:birth_year>
C) Minimalist, where the name of the predefined pattern will be automatically be used as a named group: <year> (I'm not sure if this approach is used by any library or whether it's a good one, I just though of it and added it for completeness.)

3) Extending the predefined patterns:
A) Using a dictionary: h.matchers['year'] = r'\d{4}'
B) Using a function call: macrosurl.register('myhash', '[a-f0-9]{9}')
C) Using a setting in settings.py: SURL_REGEXERS = { ... }

Option A is more Pythonic than option A, but I like option C better.

Thanks.

Alexandr Shurigin

unread,
Jun 2, 2014, 5:36:51 AM6/2/14
to django-d...@googlegroups.com, Mardini Moayad
Hi! Thank you for review.

Yes i think this would be awesome feature for beginners out of the box.

Also if add it into core, need to think a way to write ‘old-style’ clean regex urls. Maybe via any other function or regex parameter type. I think 1% of case somebody require this feature.For example by default urlex - regex url pattern, url - macros url pattern.

And about urls normalization, i think 99% people use ^ and $ in urls. This must be by default too. What you think about it?

About this.

IMHO, option C is too ugly, but option B is the most natural for a beginner to use, especially when used with a named group, see point 2.

Maybe, but if combine regex & macro this can make some problems, because of this i go simplest way and used colon.

Option A is more Pythonic than option A, but I like option C better.
There you double use option «A». I think this is a mistake.

I think anyway minimalism is good, because django uses KIS (keep it simple) principle. Add parameter type is good sometimes, but url doesn’t look clean.

As an option can review one more option - support only combined macro to allow user enter «%(name)_%(macro type)» - :product_id, :news_slug, :user_id, :post_slug, :post_id, etc.

This will make urls very clean and very strong to misspells. This feature can be managed by settings value for example named URL_MACRO_ALLOW_SHORT = False by default.

-- 
Alexandr Shurigin

Moayad Mardini

unread,
Jun 2, 2014, 6:05:56 AM6/2/14
to django-d...@googlegroups.com

Sorry for the typo. I meant to say that I think option A is more Pythonic than option B.

I personally like the approach Marc suggested, which is introducing another URL resolver, rather than modifying the existing one.

Moayad

Alexandr Shurigin

unread,
Jun 2, 2014, 6:11:22 AM6/2/14
to django-d...@googlegroups.com, Mardini Moayad, django-d...@googlegroups.com

Sorry for the typo. I meant to say that I think option A is more Pythonic than option B.

No problem :)

I used function for possibility to support parameters filtering in future. Maybe to add macro name validation or pattern.

I personally like the approach Marc suggested, which is introducing another URL resolver, rather than modifying the existing one.

Very good idea too.

But will it make any overhead or problems i don’t know for now, because know django core not soo deep for now. Use it as primary framework only for about 6 months. But backends and pipelines style is very good i think anyway.


Aymeric Augustin

unread,
Jun 2, 2014, 7:49:17 AM6/2/14
to django-d...@googlegroups.com
> Le 2 juin 2014 à 11:36, Alexandr Shurigin <alexandr...@gmail.com> a écrit :
>
> And about urls normalization, i think 99% people use ^ and $ in urls. This must be by default too. What you think about it?

Probably true for ^, but not for $ which must not be present for includes.

--
Aymeric.

Alexandr Shurigin

unread,
Jun 2, 2014, 7:55:09 AM6/2/14
to Augustin Aymeric, django-d...@googlegroups.com
really? this is my fault :)

i forget about this.

need to fix this in lib:)

But if implement same style route classes in core, there we have context - is view or included and make choose about ending $ automatically.
-- 
Alexandr Shurigin

From: Augustin Aymeric aymeric.au...@polytechnique.org
Reply: django-d...@googlegroups.com django-d...@googlegroups.com
Date: 2 июня 2014 г. at 18:49:15
To: django-d...@googlegroups.com django-d...@googlegroups.com
Subject:  Re: Make url patterns group kwargs more simple

--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.

Alexandr Shurigin

unread,
Jun 3, 2014, 9:48:14 AM6/3/14
to django-d...@googlegroups.com
I make mistake, i have context of url settings.

Added support for include function. Now it works right way, add dollar sign to end of pattern only if real view was passed. If was passed include tag, then it doesnot add dollar sign.

All works cool and pretty clean!

You must try it :)
Reply all
Reply to author
Forward
0 new messages