简体   繁体   中英

Python, Django - how to store http requests in the middleware?

It might be that this question sounds pretty silly but I can not figure out how to do this I believe the simplest issue (because just start learning Django).

What I know is I should create a middleware file and connect it to the settings. Than create a view and a *.html page that will show these requests and write it to the urls.

  • how can one store last (5/10/20 or any) http requests in the middleware and show them in a *.html page? The problem is I don't even know what exactly should I write into middlaware.py and views.py in the way it could be displayed in the *.html file. Ideally, this page should be also updated after the new requests occur. I read Django documentation, some other topics with middleware examples but it seems to be pretty sophisticated for me.

I would be really thankful for any insights and elucidates.

PS One more time sorry for a dummy question.

First, as you said, you need a model to save request information in database. After you have created and migrated your new model, you write your custom middleware and do what you want in process_request method:

from yourapp.models import YourModel

class CustomDebugMiddleware_first(object):
    def process_request(self, request):
         new_http_information = YourModel(http_info=INFO_YOU_WANT_TO_SAVE)
         new_http_information.save()

and then put the path to this middleware in your settings.py into MIDDLEWARE_CLASSES

You can implement your own RequestMiddleware (which plugs in before the URL resolution) or ViewMiddleware (which plugs in after the view has been resolved for the URL).

In that middleware, it's standard python. You have access to the filesystem, database, cache server, ... the same you have anywhere else in your code.

Showing the last N requests in a separate web page means you create a view which pulls the data from the place where your middleware is storing them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM