URL redirecting and rewriting in code.
Project description
# django-redirect-urls
[![Build Status](https://travis-ci.org/pmac/django-redirect-urls.svg?branch=master)](https://travis-ci.org/pmac/django-redirect-urls)
Manage URL redirects and rewrites in Django like you do other URLs: in code.
This was extracted from [bedrock](https://github.com/mozilla/bedrock/) (the code behind www.mozilla.org).
We had a lot of redirects and rewrites we wanted to move out of Apache configs and into versioned code.
This library is the result of all of that. Because it started life on a Mozilla site it does have some
features for how we make sites (e.g. locale prefixing in URLs and the `is_firefox` helper). Now that it
is a separate thing however I'm very open to adding features and helpers for a more general audience.
## Install
```bash
$ pip install django-redirect-urls
```
## Examples
With this library you can do things like:
```python
# urls.py
from redirect_urls import redirect
urlpatterns = [
redirect(r'projects/$', 'mozorg.product'),
redirect(r'^projects/seamonkey$', 'mozorg.product', locale_prefix=False),
redirect(r'apps/$', 'https://marketplace.firefox.com'),
redirect(r'firefox/$', 'firefox.new', name='firefox'),
redirect(r'the/dude$', 'abides', query={'aggression': 'not_stand'}),
]
```
There are a lot of options to the `redirect` helper. Here is the basic list:
* **pattern**: the regex against which to match the requested URL.
* **to**: either a url name that `reverse` will find, a url that will simply be returned,
or a function that will be given the request and url captures, and return the
destination.
* **permanent**: boolean whether to send a 301 or 302 response.
* **locale_prefix**: automatically prepend `pattern` with a regex for an optional locale
in the url. This locale (or None) will show up in captured kwargs as 'locale'.
* **anchor**: if set it will be appended to the destination url after a '#'.
* **name**: if used in a `urls.py` the redirect URL will be available as the name
for use in calls to `reverse()`. Does _NOT_ work if used in a `redirects.py` file.
* **query**: a dict of query params to add to the destination url.
* **vary**: if you used an HTTP header to decide where to send users you should include that
header's name in the `vary` arg.
* **cache_timeout**: number of hours to cache this redirect. just sets the proper `cache-control`
and `expires` headers.
* **decorators**: a callable (or list of callables) that will wrap the view used to redirect
the user. equivalent to adding a decorator to any other view.
* **re_flags**: a string of any of the characters: "iLmsux". Will modify the `pattern` regex
based on the documented meaning of the flags (see python re module docs).
* **to_args**: a tuple or list of args to pass to reverse if `to` is a url name.
* **to_kwargs**: a dict of keyword args to pass to reverse if `to` is a url name.
* **prepend_locale**: if true the redirect URL will be prepended with the locale from the
requested URL.
* **merge_query**: merge the requested query params from the `query` arg with any query params
from the request.
Or you can install the `redirect_urls.middleware.RedirectsMiddleware` middleware and create
`redirects.py` files in your Django apps. This will allow you to define a lot of redirects
in their own files (which will be auto-discovered) and guarantee that they'll be tested before
the rest of your URLs.
```python
# redirects.py
from redirect_urls import redirect
redirectpatterns = [
redirect(r'projects/$', 'mozorg.product'),
redirect(r'^projects/seamonkey$', 'mozorg.product', locale_prefix=False),
redirect(r'apps/$', 'https://marketplace.firefox.com'),
redirect(r'firefox/$', 'firefox.new', name='firefox'),
redirect(r'the/dude$', 'abides', query={'aggression': 'not_stand'}),
]
```
## Run The Tests
```bash
$ pip install tox
$ tox
```
## History
### 1.0 - 2018-06-01
Add support for Django 1.11 and 2.0, and drop support for earlier versions.
### 0.2 - 2017-05-09
Initial release.
[![Build Status](https://travis-ci.org/pmac/django-redirect-urls.svg?branch=master)](https://travis-ci.org/pmac/django-redirect-urls)
Manage URL redirects and rewrites in Django like you do other URLs: in code.
This was extracted from [bedrock](https://github.com/mozilla/bedrock/) (the code behind www.mozilla.org).
We had a lot of redirects and rewrites we wanted to move out of Apache configs and into versioned code.
This library is the result of all of that. Because it started life on a Mozilla site it does have some
features for how we make sites (e.g. locale prefixing in URLs and the `is_firefox` helper). Now that it
is a separate thing however I'm very open to adding features and helpers for a more general audience.
## Install
```bash
$ pip install django-redirect-urls
```
## Examples
With this library you can do things like:
```python
# urls.py
from redirect_urls import redirect
urlpatterns = [
redirect(r'projects/$', 'mozorg.product'),
redirect(r'^projects/seamonkey$', 'mozorg.product', locale_prefix=False),
redirect(r'apps/$', 'https://marketplace.firefox.com'),
redirect(r'firefox/$', 'firefox.new', name='firefox'),
redirect(r'the/dude$', 'abides', query={'aggression': 'not_stand'}),
]
```
There are a lot of options to the `redirect` helper. Here is the basic list:
* **pattern**: the regex against which to match the requested URL.
* **to**: either a url name that `reverse` will find, a url that will simply be returned,
or a function that will be given the request and url captures, and return the
destination.
* **permanent**: boolean whether to send a 301 or 302 response.
* **locale_prefix**: automatically prepend `pattern` with a regex for an optional locale
in the url. This locale (or None) will show up in captured kwargs as 'locale'.
* **anchor**: if set it will be appended to the destination url after a '#'.
* **name**: if used in a `urls.py` the redirect URL will be available as the name
for use in calls to `reverse()`. Does _NOT_ work if used in a `redirects.py` file.
* **query**: a dict of query params to add to the destination url.
* **vary**: if you used an HTTP header to decide where to send users you should include that
header's name in the `vary` arg.
* **cache_timeout**: number of hours to cache this redirect. just sets the proper `cache-control`
and `expires` headers.
* **decorators**: a callable (or list of callables) that will wrap the view used to redirect
the user. equivalent to adding a decorator to any other view.
* **re_flags**: a string of any of the characters: "iLmsux". Will modify the `pattern` regex
based on the documented meaning of the flags (see python re module docs).
* **to_args**: a tuple or list of args to pass to reverse if `to` is a url name.
* **to_kwargs**: a dict of keyword args to pass to reverse if `to` is a url name.
* **prepend_locale**: if true the redirect URL will be prepended with the locale from the
requested URL.
* **merge_query**: merge the requested query params from the `query` arg with any query params
from the request.
Or you can install the `redirect_urls.middleware.RedirectsMiddleware` middleware and create
`redirects.py` files in your Django apps. This will allow you to define a lot of redirects
in their own files (which will be auto-discovered) and guarantee that they'll be tested before
the rest of your URLs.
```python
# redirects.py
from redirect_urls import redirect
redirectpatterns = [
redirect(r'projects/$', 'mozorg.product'),
redirect(r'^projects/seamonkey$', 'mozorg.product', locale_prefix=False),
redirect(r'apps/$', 'https://marketplace.firefox.com'),
redirect(r'firefox/$', 'firefox.new', name='firefox'),
redirect(r'the/dude$', 'abides', query={'aggression': 'not_stand'}),
]
```
## Run The Tests
```bash
$ pip install tox
$ tox
```
## History
### 1.0 - 2018-06-01
Add support for Django 1.11 and 2.0, and drop support for earlier versions.
### 0.2 - 2017-05-09
Initial release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-redirect-urls-1.0.tar.gz
(11.7 kB
view details)
Built Distribution
File details
Details for the file django-redirect-urls-1.0.tar.gz
.
File metadata
- Download URL: django-redirect-urls-1.0.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5b1dec666758c7d2621ce337458a09e4b3feeb06cb9ec5d43b17d66b87ea97d |
|
MD5 | 4291f2772afcaba0651b978bf182e604 |
|
BLAKE2b-256 | 458db74b732f846eff25a15c4986097ddec499e0ce4a3c34df2f686942b56986 |
Provenance
File details
Details for the file django_redirect_urls-1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: django_redirect_urls-1.0-py2.py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4cd7818b06ccf0a5307656a33f3ecea47ee22f0cceb374ea559eb2d1402d5d8 |
|
MD5 | c4fa2eb537e02ca03edb45a9f21455bb |
|
BLAKE2b-256 | d2b54bb94608dd172bdc164837b7db0ab3942c4b2780894c3ea254ad84541028 |