Git Product home page Git Product logo

django-unslashed's Introduction

django-unslashed

Version Build Status Downloads License

This middleware provides the inverse of the Django CommonMiddleware APPEND_SLASH feature. It can automatically remove trailing URL slashes and 301 redirect to the non-slash-terminated URL. This behavior is performed if the initial URL ends in a slash and is invalid, removing the trailing slash produces a valid URL, and REMOVE_SLASH is set to True. Otherwise there is no effect.

For example, foo.com/bar/ will be redirected to foo.com/bar if you don't have a valid URL pattern for foo.com/bar/ but do have a valid pattern for foo.com/bar and REMOVE_SLASH=True.

Install

To install django-unslashed,

pip install django-unslashed

If you're using a requirements.txt file, add django-unslashed>=0.3.0 to it.

Usage

Modify your Django settings.py file to add unslashed.middleware.RemoveSlashMiddleware to your MIDDLEWARE_CLASSES just before or after django.middleware.common.CommonMiddleware.

MIDDLEWARE_CLASSES = (
    # ...
    'unslashed.middleware.RemoveSlashMiddleware',
    'django.middleware.common.CommonMiddleware',
    # ...
)

Set REMOVE_SLASH to True and APPEND_SLASH to False,

APPEND_SLASH = False
REMOVE_SLASH = True

If REMOVE_SLASH is False or unset, the RemoveSlashMiddleware has no effect.

Rationale

Web applications should have a URL structure which either:

  1. Uses trailing slashes and redirects to append slashes if invalid non-slashed-terminated URLs are accessed.
  2. Uses no trailing slash URLs and redirects to unslahed URLs if invalid slash terminated URLs are accessed. The prior is the Django default, while the later is possible by adding this middleware to your project.

Notes

Based closely on Django's APPEND_SLASH CommonMiddleware implementation.

Testing

$ git clone https://github.com/dghubble/django-unslashed.git
$ cd django-unslashed
$ python manage.py test unslashed
Creating test database for alias 'default'...
...
----------------------------------------------------------------------
Ran 6 tests in 0.071s

OK
Destroying test database for alias 'default'...

License

MIT License

django-unslashed's People

Contributors

altryne avatar dghubble avatar shariffy avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

django-unslashed's Issues

Doesn't work with django-cms

Unfortunately this awesome app doesn't seem to work with CMSs
It doesn't catch the 404 when returned by CMS :(

Broken in recent Django versions

Attempting to use this middleware in recent Django version gives the following error: TypeError: object() takes no parameters. I'm not an expert at Django's code nor middleware, but I'm guessing this comes from a change in how a Middleware class inherits.
This error can be fixed by adding from django.utils.deprecation import MiddlewareMixin to the top of the middleware.py file and changing the class signature to class RemoveSlashMiddleware(MiddlewareMixin):; which is what CommonMiddleware does.
I would be willing to make this change and PR, but I want to see if the original creator @dghubble is willing to merge and if the pip package could be updated.

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.