Giter Club home page Giter Club logo

django-redirect-urls's Introduction

django-redirect-urls

Build Status

Manage URL redirects and rewrites in Django like you do other URLs: in code.

This was extracted from 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

$ pip install django-redirect-urls

Examples

With this library you can do things like:

# 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.

# 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

$ 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.

django-redirect-urls's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

escattone jayvdb

django-redirect-urls's Issues

In redirect, change locale_prefix default to False

In redirect, the default value for locale_prefix is True. This expects that a URL will have an optional locale prefix, such as /en-US/the/page, and that the locale should be automatically pre-pended to the resulting redirect pattern. This is probably the right default for many Mozilla projects, including Bedrock and Kuma, but not in a general redirect library.

One solution would be to make locale_prefix default to False in redirect, and add a wrapper functionlocale_redirect that has the old default. This could grow an optional validation routine, for example you could pass a list of expected locales, and if the prefix doesn't match, it could not be treated as a locale. This would avoid treating paths like /foo/bar as locale=foo/.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.