Django Remove WWW¶
Django Remove WWW is a Django middleware application that removes the WWW subdomain.
The middleware inspects the request’s host for the www
subdomain, and redirects if REMOVE_WWW
is True
. It silently passes if PREPEND_WWW
is also True
. For some reason, Django won’t include a REMOVE_WWW
setting. Thanks to Daniel Ryan’s GitHub Gist for some inspiration.
Install¶
$ pip install django-removewww
Add to settings.py
.
MIDDLEWARE = [
# ...
'removewww.middleware.RemoveWwwMiddleware',
]
REMOVE_WWW = True
Adding to INSTALLED_APPS
is unnecessary unless you want to run the tests.
Contents¶
Install¶
Install with the pip package manager.
$ mkvirtualenv myvenv -p python3
$ pip install django
$ pip install django-removewww
After creating a project, add removewww.middleware.RemoveWwwMiddleware
to MIDDLEWARE
in settings.py
. Set the REMOVE_WWW
boolean to True
.
MIDDLEWARE = [
# ...
'removewww.middleware.RemoveWwwMiddleware',
]
REMOVE_WWW = True
Adding to INSTALLED_APPS
is unnecessary unless you want to run the tests.
The middleware is compatible with pre-Django 1.10-style middleware because it inherits from django.utils.deprecation.MiddlewareMixin
.
MIDDLEWARE_CLASSES = (
# ...
'removewww.middleware.RemoveWwwMiddleware',
)
REMOVE_WWW = True
Remember to update your requirements.txt
file. In your project directory:
$ pip freeze > requirements.txt
Documentation¶
Full documentation is available online.
However, you can also build the documentation from source. Enter your virtual environment.
$ workon myvenv
Clone the code repository.
$ git clone git@github.com:richardcornish/django-removewww.git
$ cd django-removewww/
Install Sphinx, sphinx-autobuild
, and sphinx_rtd_theme
.
$ pip install sphinx sphinx-autobuild sphinx_rtd_theme
Create an HTML build.
$ (cd docs/ && make html)
Or use sphinx-autobuild
to watch for live changes.
$ sphinx-autobuild docs/ docs/_build_html
Open 127.0.0.1:8000.
Tests¶
Continuous integration test results are available online.
However, you can also test the source code.
$ workon myvenv
$ django-admin test removewww.tests --settings="removewww.tests.settings"
Creating test database for alias 'default'...
..........
----------------------------------------------------------------------
Ran 10 tests in 0.009s
OK
Destroying test database for alias 'default'...
A bundled settings file allows you to test the code without even creating a Django project.