簡體   English   中英

如何在 Django 通道消費者中訪問 URL 參數

[英]How to Access URL Params in Django Channels Consumers

如果我在routing.py中有以下 url:

/rooms/<int:id>/

如何使用 JSONWebsocketConsumer 訪問 id 參數?

this.scope['url_path']['kwargs']['id']

要訪問您可以使用的任何查詢參數;

例如套接字 URL = ws://localhost:8000/notifications?id=11

from urllib.parse import parse_qsl
    
query_params = dict(parse_qsl(self.scope['query_string'].decode('utf-8')))
print(query_params['id'])

使用從查詢參數中獲取 ID;

self.scope['path'].split('/')[-2]

我認為文檔需要更新,“url_path”對我不起作用,但“url_route”對我有用。

self.scope['url_route']['kwargs']['id']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM