感谢之前的回答,我做了这个:
#middleware.py (Under the Analytics App)
from django.http import HttpResponseNotFound, HttpRequest
from analytics.models import Site_Error
class Catch404Middleware(object):
def process_response(self, request, response):
if isinstance(response, HttpResponseNotFound):
try:
new_save,created = Site_Error.objects.get_or_create(error=request.path)
new_save.times += 1
new_save.save()
except:
new_save = False
return response
#models.py (Ananlytics App)
class Site_Error(models.Model):
error = models.CharField(max_length=8000)
times = models.IntegerField(default=0)
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('-timestamp',)
verbose_name = "Error"
verbose_name_plural = "Errors"
def __unicode__(self):
return self.error
#in settings.py
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
'analytics.middleware.Catch404Middleware',
)
此代码会将所有请求的路径保存到Site_Error
模型中。你可以用这个做更多的事情,所以我建议你查看关于中间件的 Django 文档,就像之前的答案所推荐的那样。