Reputation: 623
I have a Django app and that work perfect, I want get all request 404 for print in terminal.
like: 127.0.0.1:8000/admin/hello # result is 404 because I haven't this URL
I want get /admin/hello
because I haven't this URL.
How can I do?
I mean :
User enter 127.0.0.1:8000/admin/hello
then terminal set a= /admin/hello
and print(a)
Upvotes: 0
Views: 98
Reputation: 623
Thanks from @ruddra for answer.
I explain simple:
I add to views.py:
from django.utils.deprecation import MiddlewareMixin
class BrokenLinkMiddleware(MiddlewareMixin):
def process_response(self, request, response):
if response.status_code == 404 : # for production
domain = request.get_host()
path = request.get_full_path()
print('path : ',path)
# print('domain : ',domain)
ua = request.META.get('HTTP_USER_AGENT', '<none>')
ip = request.META.get('REMOTE_ADDR', '<none>')
# Store response in Database
# print('ua:',ua)
# print('ip:',ip)
return response
Then add to settings.py segment MIDDLEWARE :
MIDDLEWARE = [
...
'MynameAPP.views.BrokenLinkMiddleware'
...
]
And finish work. thanks again @ruddra.
Upvotes: 0
Reputation: 51988
You can create a middleware for this. Here is an example based on BrokenLinkEmailMiddleware
implementation:
from django.utils.deprecation import MiddlewareMixin
class BrokenLinkMiddleware(MiddlewareMixin):
def process_response(self, request, response):
if response.status_code == 404 and not settings.DEBUG: # for production
domain = request.get_host()
path = request.get_full_path()
referer = request.META.get('HTTP_REFERER', '')
if not self.is_ignorable_request(request, path, domain, referer):
ua = request.META.get('HTTP_USER_AGENT', '<none>')
ip = request.META.get('REMOTE_ADDR', '<none>')
# Store response in Database
YourModel.objects.create(domain=domain, path=path, ua=ua, ip=ip, referer=referer)
return response
And add it your settings:
MIDDLEWARE = [
...
'path.to.BrokenLinkMiddleware',
]
Upvotes: 2