Can the simplified syntax replace regular expressions?

I believe you can completely switch to the simplified syntax and avoid using regular expressions for pattern matching altogether. Regular expressions might seem to be more powerful, but they sacrifices readability. They also have their limitations.

Consider the previous year pattern example. Some clever folks might write the regular expression as ^year/(\d{4})/. But what about year AD 793 (when Vikings start raiding Ireland) or AD 11234 (the arrival of space Vikings to earth perhaps?) or any other non-four digit year?

The simplified pattern year/<int:year>/ can match all those years and more. You could add a check for a valid year inside your view as follows:

class YearView(View): def ...

Get Django Design Patterns and Best Practices - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.