Re: Getting AttributeError: module 'asyncio' has no attribute '_get_running_loop'

2019-07-07 Thread Rohit Chopra
Hi Andrew,

Just a small doubt, why same code is working on my local system.
Both local and server have same  version of requirements mentioned in
requirements.txt

Rohit

On Sun 7 Jul, 2019, 9:36 PM Andrew Godwin,  wrote:

> Hi Rohit,
>
> This is my fault - we made a change in the "asgiref" library that
> inadvertently removed Python 3.5 support. Channels still needs to support
> it (even though Django doesn't).
>
> If you update to asgiref 3.1.4, which I've just released, that should fix
> the issue.
>
> Andrew
>
> On Sun, Jul 7, 2019 at 8:52 AM Rohit Chopra 
> wrote:
>
>> Hi All,
>>
>> I am using channels to implement WebSockets.
>> I am trying to send data through WebSockets from *post_save *django
>> signal.
>> Below is my code.
>>
>> **signals.py**
>>
>>
>> @receiver(post_save, sender=CheckIn)
>> def send_data_on_save(sender, instance, **kwargs):
>> channel_layer = get_channel_layer()
>> stats = get_stats()
>> async_to_sync(channel_layer.group_send)(
>> 'dashboard',
>> {
>> 'type': 'send_data',
>> 'message': stats
>> }
>> )
>>
>>
>> analytics = get_analytics()
>> async_to_sync(channel_layer.group_send)(
>> 'analytic',
>> {
>> 'type': 'send_data',
>> 'message': analytics
>> }
>> )
>>
>>
>>
>> **consumers.py**
>>
>>
>> class DashboardConsumer(WebsocketConsumer):
>> def connect(self):
>> self.room_group_name = 'dashboard'
>>
>>
>> # Join room group
>> async_to_sync(self.channel_layer.group_add)(
>> self.room_group_name,
>> self.channel_name
>> )
>>
>>
>> self.accept()
>>
>>
>> def disconnect(self, close_code):
>> # Leave room group
>> async_to_sync(self.channel_layer.group_discard)(
>> self.room_group_name,
>> self.channel_name
>> )
>>
>>
>> # Receive message from WebSocket
>> def receive(self, text_data):
>> text_data_json = json.loads(text_data)
>> message = text_data_json['message']
>>
>>
>> # Send message to room group
>> async_to_sync(self.channel_layer.group_send)(
>> self.room_group_name,
>> {
>> 'type': 'send_data',
>> 'message': message
>> }
>> )
>>
>>
>> # Receive message from room group
>> def send_data(self, event):
>> message = event['message']
>>
>>
>> # Send message to WebSocket
>> self.send(text_data=json.dumps({
>> 'message': message
>> }))
>>
>>
>>
>> this same piece of code is working on my local machine(windows) but when
>> i am trying to run this code on server(ubuntu 16.04) i am getting bellow
>> error:
>>
>> **Traceback**
>>
>>
>>
>> Exception inside application: module 'asyncio' has no attribute
>> '_get_running_loop'
>> File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
>> result = coro.throw(exc)
>> File
>> "/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/sessions.py"
>> , line 183, in __call__
>> return await self.inner(receive, self.send)
>> File
>> "/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/middleware.py"
>> , line 41, in coroutine_call
>> await inner_instance(receive, send)
>> File
>> "/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/consumer.py"
>> , line 59, in __call__
>> [receive, self.channel_receive], self.dispatch
>> File
>> "/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/utils.py"
>> , line 52, in await_many_dispatch
>> await dispatch(result)
>> File
>> "/home/user1/demowebapps/env/lib/python3.5/site-packages/asgiref/sync.py"
>> , line 145, in __call__
>> return await asyncio.wait_for(future, timeout=None)
>> File "/usr/lib/python3.5/

Getting AttributeError: module 'asyncio' has no attribute '_get_running_loop'

2019-07-07 Thread Rohit Chopra
Hi All,

I am using channels to implement WebSockets.
I am trying to send data through WebSockets from *post_save *django signal.
Below is my code.

**signals.py**


@receiver(post_save, sender=CheckIn)
def send_data_on_save(sender, instance, **kwargs):
channel_layer = get_channel_layer()
stats = get_stats()
async_to_sync(channel_layer.group_send)(
'dashboard',
{
'type': 'send_data',
'message': stats
}
)


analytics = get_analytics()
async_to_sync(channel_layer.group_send)(
'analytic',
{
'type': 'send_data',
'message': analytics
}
)



**consumers.py**


class DashboardConsumer(WebsocketConsumer):
def connect(self):
self.room_group_name = 'dashboard'


# Join room group
async_to_sync(self.channel_layer.group_add)(
self.room_group_name,
self.channel_name
)


self.accept()


def disconnect(self, close_code):
# Leave room group
async_to_sync(self.channel_layer.group_discard)(
self.room_group_name,
self.channel_name
)


# Receive message from WebSocket
def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']


# Send message to room group
async_to_sync(self.channel_layer.group_send)(
self.room_group_name,
{
'type': 'send_data',
'message': message
}
)


# Receive message from room group
def send_data(self, event):
message = event['message']


# Send message to WebSocket
self.send(text_data=json.dumps({
'message': message
}))



this same piece of code is working on my local machine(windows) but when i 
am trying to run this code on server(ubuntu 16.04) i am getting bellow 
error:

**Traceback**



Exception inside application: module 'asyncio' has no attribute 
'_get_running_loop'
File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File 
"/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/sessions.py"
, line 183, in __call__
return await self.inner(receive, self.send)
File 
"/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/middleware.py"
, line 41, in coroutine_call
await inner_instance(receive, send)
File 
"/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/consumer.py"
, line 59, in __call__
[receive, self.channel_receive], self.dispatch
File 
"/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/utils.py", 
line 52, in await_many_dispatch
await dispatch(result)
File 
"/home/user1/demowebapps/env/lib/python3.5/site-packages/asgiref/sync.py", 
line 145, in __call__
return await asyncio.wait_for(future, timeout=None)
File "/usr/lib/python3.5/asyncio/tasks.py", line 373, in wait_for
return (yield from fut)
File "/usr/lib/python3.5/asyncio/futures.py", line 361, in __iter__
yield self  # This tells Task to wait for completion.
File "/usr/lib/python3.5/asyncio/tasks.py", line 296, in _wakeup
future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
result = self.fn(*self.args, **self.kwargs)
File 
"/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/db.py", 
line 14, in thread_handler
return super().thread_handler(loop, *args, **kwargs)
File 
"/home/user1/demowebapps/env/lib/python3.5/site-packages/asgiref/sync.py", 
line 164, in thread_handler
return func(*args, **kwargs)
File 
"/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/consumer.py"
, line 105, in dispatch
handler(message)
File 
"/home/user1/demowebapps/env/lib/python3.5/site-packages/channels/generic/websocket.py"
, line 39, in websocket_connect
self.connect()
File "/home/user1/demowebapps/vmsbackend/check_in/consumers.py", line 57
, in connect
self.channel_name
File 
"/home/user1/demowebapps/env/lib/python3.5/site-packages/asgiref/sync.py", 
line 41, in __call__
if asyncio._get_running_loop() is not None:
module 'asyncio' has no attribute '_get_running_loop'





also when i am sending data normally from
javascript  .send()


function it is working normally on server but when post_save signal is 
called then only i get this error on server. this is working fine on my 
local system.
I tried removing *async_to_sync *from signals then it is not giving error 
but it is not transmitting data through web socket.
then it is giving:

./check_in/signals.py:47: RuntimeWarning: Coroutine
'RedisChannelLayer.group_send' was never 

How To style a Django formset for fileField

2017-04-13 Thread Rohit Chopra
stackoverflow question 





down votefavorite 


I have this formset.

instance = get_object_or_404(Post, user = request.user, slug = slug )
files = file.objects.all().filter(Post=instance)FormSet = 
modelformset_factory(file, fields=('file',), can_delete=True)
formset=FormSet(request.POST or None, request.FILES or None, queryset=files)

models.py:

class file(models.Model):
file = models.FileField(upload_to = upload_location, blank = True, null = 
True)
Post = models.ForeignKey(Post, blank = True, null = True)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True, blank = 
True, null = True)

def filename(self):
f = self.file.name
f1 = f.split('/', 3)[-1]
return f1

def __unicode__(self):
return self.file.name

def __str__(self):
return self.file.name

thats how i used it in my template:

 {{ formset.management_form }}
 {% for form in formset %}
 {{form.file}}
 {{form.DELETE.label}}
 {{form.DELETE}}
 {% endfor %}

and it shows like this in my browser:

[image: look] 

i tried every this but i could not able to to get rid of currently and i 
literally do not know how to style this. please help me guys.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/372952eb-6a2c-4422-9706-00a1a6dbc444%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.