У меня возникли небольшие проблемы с возможностью сопоставления кнопки как с загружаемым ответом, так и с сообщением через message_user()
. Чтобы дать больше контекста, вот как выглядит моя admin/user
страница:
![enter image description here](https://i.stack.imgur.com/b01Of.png)
Проблема главным образом в кнопках загрузки. Эти кнопки созданы так (только для простоты показаны .zip one):
def download_zip(self, user):
if (
user.completed_agreement
and user.completed_survey
and user.completed_questionnaire
and user.completed_feedback
):
return format_html(
'<a class="button" href="{}" target="_blank">Download</a>',
reverse("admin:username-download-zip", args=[user]),
)
else:
return format_html(
'<a class="button" disabled="disabled" title="Cannot download user who has not completed full survey.">Download</a>',
)
И при нажатии вызывает следующую функцию:
def download_user_zip(self, request, user):
""" Downloades the given username as a zip format. """
# Message to display to the user.
self.message_user(
request, mark_safe(f"Downloading files for the username <b>{user}</b>."),
)
# Grabs the user generated zip file as a memory file.
mem_file = download_user_zip_file(user)
# Generates the response object.
response = HttpResponse(
mem_file.getvalue(), content_type="application/x-zip-compressed"
)
response["Content-Disposition"] = f"attachment; filename={user}.zip"
# Saves the memory file to response buffer.
response.write(mem_file)
# Returns the response.
return response
На данный момент, функция работает с точки зрения загрузки. Файл .zip
открывается на другой вкладке и начинает загрузку. Моя проблема заключается в том, что следующее больше не будет работать:
self.message_user(request, mark_safe(f"Downloading files for the username <b>{user}</b>."),)
Чтобы это работало, я должен вручную обновить sh страницу после нажатия на нее. Мне было интересно, есть ли способ как отобразить сообщение, так и вернуть ответ пользователю по нажатию кнопки. Буду признателен за любую помощь по этому вопросу!