Hi,

I don't think that the 'including' URLs part forms a problem here.

For the given example, it should be easily doable

> from django.conf.urls import simple_url
> from . import views
> urlpatterns = [
>     simple_url('articles/2003/', views.special_case_2003),
>     simple_url('articles/:year)/', views.year_archive),
>     simple_url('articles/:year/:month/', views.month_archive),
>     simple_url('articles/:year/:month/:day/', views.article_detail),
> ]

The following should make it possible:

    import re

    def simple_url(route, view, *args, **kwargs):
        regex_route = re.escape(route)
        regex_route = re.sub(r':([A-Za-z0-9_-]+)', r'(?P<\1>[^/]+)', 
regex_route)
        # Anchor it to the beginning.
        regex_route = '^' + regex_route

        if not isinstance(view, (list, tuple)):
            # It's not an include...
            regex_route += '$'

        return url(regex_route, view, *args, **kwargs)

(Not 100% tested, but I tested the re.sub ;) ).

So I think it is at least possible. Whether or not it's desirable though
is another question which I think I'm not suited to answer.

Kind regards,
Sjoerd Job

On Mon, Sep 12, 2016 at 11:26:36PM -0700, Ares Ou wrote:
> Hi,
> 
> Actually flask uses a style very similar to what you want.
> 
> To my knowing, you must use this pattern for Django because it has the
> concept of *including *
> URLs.
> 
> Routing in flask:
> 
> @app.route('post/<integer:post_id>', methods=['GET'])
> def post_view(post_id=None):
>     # do something to render the post
>     return render_template('post.html', post=post)
> 
> But the problem is, you have to specify the whole URL for every view you
> define.
> 
> Django avoids this by separating URL patterns into different levels, that's
> why it uses regex to
> identify the exact path. I guess it is hard for Django to organize URLs in
> different apps by using
> such simple method.
> 
> Looking forward to more ideas!
> 
> Best regards,
> Ares Ou
> 
> *Software Engineer / Full-Stack Python Developer  |  **Phone:* (510) 328 -
> 5968
> 
> *Blog:* http://aresou.net  |  *Github:* https://github.com/aresowj  |  *Stack
> Overflow:* http://stackoverflow.com/users/5183727/ares-ou
> 
> Ares Ou
> 
> On Mon, Sep 12, 2016 at 10:20 PM, Constantine Covtushenko <
> constantine.covtushe...@gmail.com> wrote:
> 
> > Hi Emil,
> >
> > It is a very interesting idea.
> >
> > +1 from me
> >
> > On Mon, Sep 12, 2016 at 11:32 PM, Emil Stenström <e...@kth.se> wrote:
> >
> >> Hi Djangonauts,
> >>
> >> I'm just back from my second year of teaching Django to absolute
> >> beginners. The course is a combination of HTML, CSS, Python, and Django,
> >> and after five days of coding they have a real live website that they can
> >> show to friends. It's always such a great experience to see the look in
> >> their eyes when they finally understand how they can tame Django to do what
> >> they want.
> >>
> >> There's one big thing that keeps tripping them up is urls.py. When
> >> specifying URL:s I get lots of questions about the regexes that they have
> >> to specify. First: there's a strange "r" in front of each line: r"regex".
> >> That means I will have to explain string escaping to them. Then there's the
> >> "^" and "$" signs, both which requires explaining regular expressions at
> >> length. Then there's [0-9]+ and finally there's the parenthesis around the
> >> regex. All in all, looking at URLs from a beginners perspective, they are a
> >> bunch of hieroglyphs, and very hard for beginners to grasp right away.
> >>
> >> I'm not suggesting that urls.py are changed for most users, I'm
> >> suggesting that *simple_url* method (name inspired by simple_tag) is added
> >> to django.conf.urls that new users can use to get started quickly. This
> >> means that most beginners can postpone learning regexes a couple of months.
> >> The exact syntax that simple_url takes isn't important to me, as long it's
> >> a lot more beginner friendly than what we have today:
> >>
> >> https://docs.djangoproject.com/en/1.10/topics/http/urls/#example
> >>
> >> Just to get the ideas flowing, here's a suggestion, inspired by rails
> >> (again, exact syntax isn't important to me, simplicity to beginners is, so
> >> feel free to suggest something else if you agree that this is an important
> >> issue):
> >>
> >> from django.conf.urls import simple_url
> >> from . import views
> >> urlpatterns = [
> >>     simple_url('articles/2003/', views.special_case_2003),
> >>     simple_url('articles/:year)/', views.year_archive),
> >>     simple_url('articles/:year/:month/', views.month_archive),
> >>     simple_url('articles/:year/:month/:day/', views.article_detail),
> >> ]
> >>
> >> All parameters would be passed to the view as keyword parameters with the 
> >> name given and as a string, and validation would happen there instead.
> >>
> >> I'm thinking there should be no settings with simple_url, and that any 
> >> more advanced use-case should switch to using url instead.
> >>
> >> Two questions:
> >>
> >> A) What do you think about the prospect of simplifying urls.py for 
> >> beginners?
> >> B) What do you think about the specific suggestion to mimic Rails urls 
> >> with a simple_url tag?
> >>
> >> Thanks for reading!
> >>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Django developers (Contributions to Django itself)" group.
> >> To unsubscribe from this group and stop receiving emails from it, send an
> >> email to django-developers+unsubscr...@googlegroups.com.
> >> To post to this group, send email to django-developers@googlegroups.com.
> >> Visit this group at https://groups.google.com/group/django-developers.
> >> To view this discussion on the web visit https://groups.google.com/d/ms
> >> gid/django-developers/3d002c25-9d98-49b1-b84c-55bc39c6a0f9%
> >> 40googlegroups.com
> >> <https://groups.google.com/d/msgid/django-developers/3d002c25-9d98-49b1-b84c-55bc39c6a0f9%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 developers (Contributions to Django itself)" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to django-developers+unsubscr...@googlegroups.com.
> > To post to this group, send email to django-developers@googlegroups.com.
> > Visit this group at https://groups.google.com/group/django-developers.
> > To view this discussion on the web visit https://groups.google.com/d/
> > msgid/django-developers/CAK52boUa_D-%2BVaf6VPgrA0YDAK4CWFbjFSWxV3Ri
> > Vf-JEDXm1Q%40mail.gmail.com
> > <https://groups.google.com/d/msgid/django-developers/CAK52boUa_D-%2BVaf6VPgrA0YDAK4CWFbjFSWxV3RiVf-JEDXm1Q%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 developers  (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-developers+unsubscr...@googlegroups.com.
> To post to this group, send email to django-developers@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-developers.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-developers/CAFCGC%3DaX-0fvW9K%3D6dZa_wMAQVDLUAKc6mKoSzaOfCObhbmViw%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/20160913073240.GB1775%40sjoerdjob.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to