All Projects → phpdude → Django Macros Url

phpdude / Django Macros Url

Django Macros URL. Routing must be simple as possible

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Django Macros Url

React Router Component
Declarative router component for React.
Stars: ✭ 879 (+626.45%)
Mutual labels:  router, routing
Universal Router
A simple middleware-style router for isomorphic JavaScript web apps
Stars: ✭ 1,598 (+1220.66%)
Mutual labels:  router, routing
Url Mapper
Take a URL and map to functions, parsing params
Stars: ✭ 39 (-67.77%)
Mutual labels:  router, routing
Storeon Async Router
Asynchronous router for Storeon. It provides possibility for prefetch the data, lazy load, navigation cancellation, and routes modification on the fly.
Stars: ✭ 22 (-81.82%)
Mutual labels:  router, routing
Literoute
LiteRoute is easy transition for your app. Written on Swift 4
Stars: ✭ 90 (-25.62%)
Mutual labels:  router, routing
Bidi
Bidirectional URI routing
Stars: ✭ 941 (+677.69%)
Mutual labels:  router, routing
Urljects
Deprecated! (Django routing without urls.py files, inspired by Flask.)
Stars: ✭ 53 (-56.2%)
Mutual labels:  django, router
Freerouting
Advanced PCB autorouter (finally, no Java installation required)
Stars: ✭ 307 (+153.72%)
Mutual labels:  router, routing
Djurl
Simple yet helpful library for writing Django urls by an easy, short and intuitive way.
Stars: ✭ 85 (-29.75%)
Mutual labels:  django, routing
Lit Element Router
A LitElement Router (1278 bytes gzip)
Stars: ✭ 85 (-29.75%)
Mutual labels:  router, routing
Micro Router
🚉 A tiny and functional router for Zeit's Micro
Stars: ✭ 621 (+413.22%)
Mutual labels:  router, routing
Php Router
simple and flexible Router class for PHP. with Controllers and Middlewares support.
Stars: ✭ 111 (-8.26%)
Mutual labels:  router, routing
Router
🛣 Simple Navigation for iOS
Stars: ✭ 438 (+261.98%)
Mutual labels:  router, routing
Routing
The Routing component maps an HTTP request to a set of configuration variables.
Stars: ✭ 7,080 (+5751.24%)
Mutual labels:  router, routing
Graphpath
Graphpath generates an ASCII network diagram from the route table of a Unix/Linux
Stars: ✭ 321 (+165.29%)
Mutual labels:  router, routing
Restify Router
A router interface for restify that lets you aggregate route definitions and apply to a restify server
Stars: ✭ 45 (-62.81%)
Mutual labels:  router, routing
Fluro
Fluro is a Flutter routing library that adds flexible routing options like wildcards, named parameters and clear route definitions.
Stars: ✭ 3,372 (+2686.78%)
Mutual labels:  router, routing
Cortex
Routing system for WordPress
Stars: ✭ 300 (+147.93%)
Mutual labels:  router, routing
Corenavigation
📱📲 Navigate between view controllers with ease. 💫 🔜 More stable version (written in Swift 5) coming soon.
Stars: ✭ 69 (-42.98%)
Mutual labels:  router, routing
Phprouter
PhpRouter is a powerful, minimal, and very fast HTTP URL router for PHP projects
Stars: ✭ 97 (-19.83%)
Mutual labels:  router, routing

Django Macros URL v0.4.0 - Routing must be simple as possible

Django Macros URL makes it easy to write (and read) URL patterns in your Django applications by using macros.

You can combine your prefixes with macro names with an underscore, for example, you can use a macro :slug and :product_slug. They both will be compiled to same regex pattern with their group names of course. Multiple underscores accepted too.

Build Status

Supported macros by default

slug - [\w-]+
year - \d{4}
month - (0?([1-9])|10|11|12)
day - ((0|1|2)?([1-9])|[1-3]0|31)
id - \d+
pk - \d+
page - \d+
uuid - [a-fA-F0-9]{8}-?[a-fA-F0-9]{4}-?[1345][a-fA-F0-9]{3}-?[a-fA-F0-9]{4}-?[a-fA-F0-9]{12}

If you want to offer more macros by default, you can fork and make a pull request.

Installation

You can install the library with PyPI.

pip install django-macros-url

Usage

Django Macros URLs used the same way as Django standard URLs. You just import this and declare your patterns with macros.

Also, you can register new macro (or maybe you want to replace default macro with your like regex pattern) with macrosurl.register(macro, pattern) method.

An example of registration.

import macrosurl

macrosurl.register('myhash', '[a-f0-9]{9}')

urlpatterns = patterns(
    'yourapp.views',
    macrosurl.url('^:myhash/$', 'myhash_main'),
    macrosurl.url('^news/:news_myhash/$', 'myhash_news'),
)

Feel free to register custom macro anywhere (i do it in main urls.py file). Macros URLs uses lazy initialization. Macros will be compiled only on the first request.

URL normalization

Once Macros URL completed 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 $

This makes your URLs always very strong to adding any unexpected params into a path.

Auto-calling as_view() on CBV objects.

Library check type of view and if a view is type object with defined 'as_view' function, call this. This allows you omit ".as_view()" calls in your urls.py files. But you can call this manual with params if you need.

This feature helps you to keep your urls.py files clean as possible. I hope you like this feature!

Examples

Macros URL example urls.py file

from django.conf.urls import patterns
from macrosurl import url
from project.portal.views import IndexView

urlpatterns = patterns(
    'yourapp.views',
    url('^:category_slug/$', 'category'),
    url(':category_slug/:product_slug/', 'category_product'),
    url(':category_slug/:product_slug/:variant_id', 'category_product_variant'),
    url('news/', 'news'),
    url('news/:year/:month/:day', 'news_date'),
    url('news/:slug', 'news_entry'),
    url('^order/:id$', 'order'),
    url('^$', IndexView),
)

Standard Django urls example

from django.conf.urls import patterns, url
from project.portal.views import IndexView


urlpatterns = patterns(
    'yourapp.views',
    url('^(?P<category_slug>[\w-]+>)/$', 'category'),
    url('^(?P<category_slug>[\w-]+>)/(?P<product_slug>[\w-]+>)/$', 'category_product'),
    url('^(?P<category_slug>[\w-]+>)/(?P<product_slug>[\w-]+>)/(?P<variant_id>\d+>)$', 'category_product_variant'),
    url('^news/$', 'news'),
    url('^news/(?P<year>\d{4}>)/(?P<month>(0?([1-9])|10|11|12)>)/(?P<day>((0|1|2)?([1-9])|[1-3]0|31)>)$', 'news_date'),
    url('^news/(?P<slug>[\w-]+>)$', 'news_entry'),
    url('^order/(?P<id>\d+>)$', 'order'),
    url('^$', IndexView.as_view()),
)

I think you understand the difference of ways :)

Routing must be simple! ;-)

I think raw URL regexp patterns needed in 1% case only. I prefer simple way to write (and read, this is important) fancy clean URLs.

Contributor

Alexandr Shurigin

You are welcome to contribute by PR.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].