Это представление, которое я попробовал первым:
Код: Выделить всё
def redirected_page(request):
data = match_algo()
context = {
'data' : json.dumps(data),
'test' : 'test'
}
return render(request , 'redirected_page.html' , context )
Это мой потребитель.py:
Код: Выделить всё
class LoadingConsumer(AsyncWebsocketConsumer):
async def connect(self):
await self.accept()
# Send "loading..." message when the connection is established
await self.send(text_data=json.dumps({
'message': 'loading...',
"status" : False
}))
data = match_algo()
# Send "loading completed" message after processing is done
await self.send(text_data=json.dumps({
'message': "loaded",
"status" : True,
# "matches" : data
}))
# Close the connection after sending the final message
await self.close()
Код: Выделить всё
data = match_algo()
Это действительно работает. Но когда я запускаю match_algo через несколько секунд, веб-сокеты отключаются. Но функция match_algo работает. Поскольку соединение через веб-сокет закрывается. Я не могу ни получить результат, ни отобразить его.
Можно ли как-нибудь это исправить? Или есть какой-нибудь лучший метод??
redirect.html:
Код: Выделить всё
Therapist Details
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
}
body {
margin : 80px 150px
}
Therapist Details
Matching Therapist suitable for you. Please wait.
const loadingMessage = document.getElementById('loadingMessage');
const socket = new WebSocket('ws://' + window.location.host + '/ws/loading/');
socket.onopen = function(event) {
console.log('WebSocket opened with code:', event.code);
};
socket.onmessage = function(event) {
const data = JSON.parse(event.data);
console.log(data.message)
console.log(data.matches)
if (data.status) {
const tableHTML = `
Name
Specialty
Experience
Contact
John Doe
Clinical Psychology
10 years
johndoe@example.com
Jane Smith
Marriage and Family Therapy
8 years
janesmith@example.com
Michael Johnson
Child and Adolescent Therapy
12 years
michaeljohnson@example.com
`;
// Add the table to the DOM
const tableContainer = document.getElementById('therapist-table-container');
tableContainer.innerHTML = tableHTML;
} else {
// Hide the table if data.status is false
const tableContainer = document.getElementById('therapist-table-container');
tableContainer.innerHTML = 'Matching Therapist suitable for you. Please wait.';
}
};
socket.onclose = function(event) {
console.log('WebSocket closed with code:', event.code);
};
Подробнее здесь: https://stackoverflow.com/questions/789 ... time-witho