|
Revision 15, 0.7 kB
(checked in by nobu, 10 months ago)
|
refs #8 common.FlatPage?の追加。
django.contrib.flatpagesを使うか迷ったものの、
欲しい機能と若干違うところがあったので、
コピペで実装。
|
| Line | |
|---|
| 1 |
# vim: encoding=utf-8 : |
|---|
| 2 |
|
|---|
| 3 |
from django.conf import settings |
|---|
| 4 |
from django.http import Http404 |
|---|
| 5 |
|
|---|
| 6 |
from common.views import flatpage |
|---|
| 7 |
|
|---|
| 8 |
class FlatpageFallbackMiddleware(object): |
|---|
| 9 |
def process_response(self, request, response): |
|---|
| 10 |
if response.status_code != 404: |
|---|
| 11 |
return response # No need to check for a flatpage for non-404 responses. |
|---|
| 12 |
try: |
|---|
| 13 |
return flatpage(request, request.path) |
|---|
| 14 |
# Return the original response if any errors happened. Because this |
|---|
| 15 |
# is a middleware, we can't assume the errors will be caught elsewhere. |
|---|
| 16 |
except Http404: |
|---|
| 17 |
return response |
|---|
| 18 |
except: |
|---|
| 19 |
if settings.DEBUG: |
|---|
| 20 |
raise |
|---|
| 21 |
return response |
|---|