Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phpdude/django-macros-url
Django Macros URL. Routing must be simple as possible
https://github.com/phpdude/django-macros-url
django django-routing python router routing urls
Last synced: 4 days ago
JSON representation
Django Macros URL. Routing must be simple as possible
- Host: GitHub
- URL: https://github.com/phpdude/django-macros-url
- Owner: phpdude
- Created: 2014-05-31T18:03:10.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-08-08T13:14:10.000Z (over 7 years ago)
- Last Synced: 2024-05-22T15:12:18.228Z (7 months ago)
- Topics: django, django-routing, python, router, routing, urls
- Language: Python
- Homepage: http://phpdude.github.io/django-macros-url
- Size: 104 KB
- Stars: 139
- Watchers: 6
- Forks: 14
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
- Changelog: CHANGELOG.markdown
Awesome Lists containing this project
README
# [Django Macros URL](https://github.com/phpdude/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](https://travis-ci.org/phpdude/django-macros-url.svg?branch=master)](https://travis-ci.org/phpdude/django-macros-url)
### 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.
```python
import macrosurlmacrosurl.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
```python
from django.conf.urls import patterns
from macrosurl import url
from project.portal.views import IndexViewurlpatterns = 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
```python
from django.conf.urls import patterns, url
from project.portal.views import IndexViewurlpatterns = patterns(
'yourapp.views',
url('^(?P[\w-]+>)/$', 'category'),
url('^(?P[\w-]+>)/(?P[\w-]+>)/$', 'category_product'),
url('^(?P[\w-]+>)/(?P[\w-]+>)/(?P\d+>)$', 'category_product_variant'),
url('^news/$', 'news'),
url('^news/(?P\d{4}>)/(?P(0?([1-9])|10|11|12)>)/(?P((0|1|2)?([1-9])|[1-3]0|31)>)$', 'news_date'),
url('^news/(?P[\w-]+>)$', 'news_entry'),
url('^order/(?P\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](https://github.com/phpdude/)
You are welcome to contribute by PR.