I am creating a chat like facebook chat... so in views.py of my Chat Application, I need to retrieve only the last messages every 3-4 seconds with ajax poll ( the latency is not a problem for me ).
If I can disable some Middlewares and some Request Context in this view, the response will be faster... no ?
My question is:
Is there a way to disable some Middlewares and some Request Context in some views ?
This is not likely to be feasible. Best to have a second Django project or WSGI app to handle these requests.
Related
Background:
I am creating a chat app using Django Channels that is the pilot for a more complicated app down the road that will need real-time data updates. Right now, the backend for the chat app is set up with Channels and Django REST Framework. When the chat app is opened, a websocket is created and messages are sent through the websocket, as any other basic chat app does. Also, when a message is sent, the message is posted to the REST framework to store the message in the database.
Question:
This app that I will be creating has other resources that will be posting data to the Django REST Framework, and this data needs to be shown on the front end in real-time. My idea for this is when the REST framework gets a POST request from one of the resources, the data from the POST request gets sent as a message through the websocket so the data gets updated on the frontend. Is there a way to do this? I have been struggling finding resources on this.
Yes you can and it is explained in the docs.
You can interact with the consumer through the channel layer from anywhere in your code. And this includes your DRF views, serializer, etc.
So, when you receive the message in your DRF view, you can do something like this:
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)("chat", {"type": "send_message", "message": message})
Where "chat" is the group name and send_message is a handler in your consumer that sends the message and message is the message you want to send. Could be a dict or some other serializable type.
I want to log every request sent to Django.
There are some posts online about logging middleware.
The Django documentation talks about logger configuration and it seems that I can set it up to log everything without writing middleware.
Can I log everything without middleware?
What can middleware give me that a well-tuned Django logging config cannot?
Can I log INFO level messages, with DEBUG = False and no middleware?
How much extra load does logging everything add to my app?
if want to log every request, in that case, you should use Logging in middleware
https://djangosnippets.org/snippets/428/
i use this line :
request.session.set_expiry(60)
to logout after one minute of inactivity but it logout after one minute from logging time
what can i use for this,
and thanks
Only requests which cause the session to be altered are considered activity.
Use the SESSION_SAVE_EVERY_REQUEST setting to alter the session every request if you don't mind the overhead. Other wise you can simply call it in every view ( with a decorator ) or add a middleware to do so.
I have a Django app that is using REST_Framework and I can GET, POST, PUT, etc., to it as expected.
What I would like to do now, however, is automatically POST data from my Django app to another server.
Basically, I have a situation in which an asynchronous job runs, and when it is complete, rather than wait for the other server to poll my app for the latest updates, I want my app to just POST the data automatically.
I would like to continue using the REST_Framework serializers, etc.
You could just use python-requests do post your data (towards the end of your view function).
payload = serialized_object
request.post(url, data=payload)
I have created a REST web service using Django. This web service has a log file. I'd like to log all web service (http) requests in the log file. However, the web service request handling is done by Django, I only setup url-request handlers mapping and create the request handlers (views in Django nomenclature). Is there a way to log all requests in a central point, without needing to log each request in its associated request handler (view)?
Thanks in advance.
Yes Django has a built in signals framework.
It allows you to register a function to be called everytime a request starts.
This documenation page explains how to do it step by step
Using the decorator method:
from django.core.signals import request_started
from django.dispatch import receiver
#receiver(request_started)
def my_callback(sender, **kwargs):
# log the request here
pass
Where should this code live? You can put signal handling and
registration code anywhere you like. However, you’ll need to make sure
that the module it’s in gets imported early on so that the signal
handling gets registered before any signals need to be sent. This
makes your app’s models.py a good place to put registration of signal
handlers.