Skip to main content

django-unslashed

CircleCI codecov

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.

Fork info

This is a fork from https://github.com/harshvb7/django-unslashed, which is in turn a fork of https://github.com/dghubble/django-unslashed.

Changes to this fork: * updated for Django 1.10, compatible down to 1.8, and Python 2.7, 3.3-3.5. * PyPI package name with prefix tocka- * Added CircleCI and Codecov, removed TravisCI

Install

To install django-unslashed,

pip install tocka-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.

Set UNSLASHED_USE_302_REDIRECT to True if you want to use HttpRedirect instead of HttpPermanentRedirect,

UNSLASHED_USE_302_REDIRECT = True

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

Tox

This will run tests on multiple versions of Python and Django, as defined in tox.ini.

$ git clone https://github.com/frnhr/django-unslashed.git
$ cd django-unslashed
$ pip install -r requirements-dev.txt
$ tox
...
...
...
________________________________________________________________________________________________________ summary _________________________________________________________________________________________________________
  django_master-py35: commands succeeded
  django_master-py34: commands succeeded
  django_master-py27: commands succeeded
  django110-py35: commands succeeded
  django110-py34: commands succeeded
  django110-py27: commands succeeded
  django19-py35: commands succeeded
  django19-py34: commands succeeded
  django19-py27: commands succeeded
  django18-py35: commands succeeded
  django18-py34: commands succeeded
  django18-py33: commands succeeded
  django18-py27: commands succeeded
  lint: commands succeeded
  congratulations :)

License

MIT License

Metadata

Release files for tocka-django-unslashed 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tocka-django-unslashed 0.4.1
File Size Uploaded Provenance Actions
tocka-django-unslashed-0.4.1.tar.gz 8.7 kB Without provenance Details for tocka-django-unslashed-0.4.1.tar.gz

Release files / tocka-django-unslashed-0.4.1.tar.gz

Download URL tocka-django-unslashed-0.4.1.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c46675264355732cbf1ae81673682efc94b0d6f72decd0ee09629258eccc6917
BLAKE2b-256 checksum
How to use checksums
abc3d4d91acb20cbc4543e262012b51db3805aaadb1c69476943f64bf37ad84a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for tocka-django-unslashed 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.4.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page