我正在创建多个PDF文件(不同的语言),现在我获得一条成功消息,但只有一个PDF链接。如何在包含多个链接的一行中显示成功消息?
def _download_messages(folder_file, component, language, request):
pdf_file = folder_file + '.pdf'
print(pdf_file)
if os.path.isfile(pdf_file):
ok = True
downloadlink = []
downloadlink.append("<a href='" + component.get_certificate_url(language) + "'>" + language.upper() + "</a>")
else:
ok = False
downloadlink_error = []
downloadlink_error.append(language.upper() + ": <a href='" + component.get_certificate_url_tex(
language) + "'>TEX</a> " + "<a href='" + component.get_certificate_url_log(language) + "'>LOG</a>")
if ok:
messages.add_message(
request,
messages.SUCCESS,
format_html(
component.name + "(" + component.phpp_id + ")" + " Certificate is in: " + ", ".join(downloadlink)))
return True
成功消息:Ecocontract ULTRA(0579wi03)Zertifikat erstellt in:DE (其他3种语言PDF未显示,仅显示DE)
答案 0 :(得分:0)
所以,如果我理解正确,你有:
import os
### mock ###
# Django mock https://docs.djangoproject.com/en/1.11/ref/contrib/messages/
class messages():
SUCCESS = 'SUCCESS'
@staticmethod
def add_message(r, s, t):
print(r, s, t)
class component_():
name = 'name'
phpp_id = 'phpp_id'
@staticmethod
def get_certificate_url_log(language):
return language
@staticmethod
def get_certificate_url_tex(language):
return language
@staticmethod
def get_certificate_url(language):
return language
def format_html(text):
return f'<html>{text}</html>'
### /mock ###
def _download_messages(folder_file, component, language, request):
pdf_file = f'{folder_file}.pdf'
print(pdf_file)
if os.path.isfile(pdf_file):
downloadlink = []
downloadlink.append(f'<a href="{component.get_certificate_url(language)}">"{language.upper()}"</a>')
messages.add_message(
request,
messages.SUCCESS,
format_html(f'{component.name} ({component.phpp_id}) Certificate is in: {", ".join(downloadlink)}')
)
return True
else:
downloadlink_error = []
downloadlink_error.append(f'{language.upper()}: <a href="{component.get_certificate_url_tex(language)}">TEX</a>' +
f'<a href="{component.get_certificate_url_log(language)}">LOG</a>"')
return False
folder_file = 'test'
request = 'request'
_download_messages(folder_file=folder_file, component=component_, language='DE', request=request)
_download_messages(folder_file=folder_file, component=component_, language='EN', request=request)
_download_messages(folder_file=folder_file, component=component_, language='ES', request=request)
结果(在这种情况下):
test.pdf
request SUCCESS <html>name (phpp_id) Certificate is in: <a href="DE">"DE"</a></html>
test.pdf
request SUCCESS <html>name (phpp_id) Certificate is in: <a href="EN">"EN"</a></html>
test.pdf
request SUCCESS <html>name (phpp_id) Certificate is in: <a href="ES">"ES"</a></html>
True
你想要的只是一次调用_download_messages
而不是?
然后做:
def _download_messages(folder_file, component, request, languages=('DE', 'EN', 'ES')):
res = []
for language in languages:
pdf_file = f'{folder_file}_{language}.pdf'
print(pdf_file)
if os.path.isfile(pdf_file):
downloadlink = []
downloadlink.append(f'<a href="{component.get_certificate_url(language)}">"{language.upper()}"</a>')
messages.add_message(
request,
messages.SUCCESS,
format_html(f'{component.name} ({component.phpp_id}) Certificate is in: {", ".join(downloadlink)}')
)
res.append(True)
else:
downloadlink_error = []
downloadlink_error.append(f'{language.upper()}: <a href="{component.get_certificate_url_tex(language)}">TEX</a>' +
f'<a href="{component.get_certificate_url_log(language)}">LOG</a>"')
res.append(False)
return res
folder_file = 'test'
request = 'request'
_download_messages(folder_file=folder_file, component=component_, request=request)
获得:
test_DE.pdf
request SUCCESS <html>name (phpp_id) Certificate is in: <a href="DE">"DE"</a></html>
test_EN.pdf
request SUCCESS <html>name (phpp_id) Certificate is in: <a href="EN">"EN"</a></html>
test_ES.pdf
request SUCCESS <html>name (phpp_id) Certificate is in: <a href="ES">"ES"</a></html>
[True, True, True]