Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
Simplified the readme to point to RTD
Browse files Browse the repository at this point in the history
  • Loading branch information
d0ugal committed Nov 25, 2011
1 parent ea3aa73 commit 0afb494
Showing 1 changed file with 1 addition and 62 deletions.
63 changes: 1 addition & 62 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,4 @@ Django urlmiddleware
This app allows you to define middleware in your Django project based on url
configurations rather than adding middleware globally to every single request.


Why?
========================================

Adding middleware globally is not always a good thing. You may only want it
to take effect on certain areas in your website. Third party apps can also
then include their middleware definitions in the url.py file and remove the
need for you to modify global settings.


Quick Start
========================================

Install urlmiddleware::

pip install urlmiddleware

There is no need to add it to your installed apps, however, you do need to
register one global middleware class that will then control the url based
middleware::

MIDDLEWARE_CLASSES = (
# ...
# add urlmiddleware after all other middleware.
'urlmiddleware.URLMiddleware',
)

Start adding middleware to your project in your url.py files below your normal
url definitions::

middlewarepatterns = patterns('',
url(r'^myapp/', MyMiddleWareClass),
)

A common example is using this technique to add login required to whole sub
sections of your url tree. First you need to create a LoginRequiredMiddleware::

from django.conf import settings
from django.contrib.auth.views import login
from django.http import HttpResponseRedirect

class LoginRequiredMiddleware(object):

def process_request(self, request):

login_path = settings.LOGIN_URL

if request.path != login_path and request.user.is_anonymous():
if request.POST:
return login(request)
else:
return HttpResponseRedirect('%s?next=%s' % (login_path, request.path))

Then in your urls.py file::

from myapp.middleware import LoginRequiredMiddleware

middlewarepatterns = patterns('',
url(r'^accounts/', LoginRequiredMiddleware),
)

Done!
See http://urlmiddleware.readthedocs.org/ for further documentation.

0 comments on commit 0afb494

Please sign in to comment.