我正在使用wkhtmltopdf包装器在Django 1.6中将模板生成为PDF.当我想在之后显示PDF或发送带有HttpResponse的PDF文件进行下载时它工作正常,但我想要做的是在我的tmp文件夹中创建文件并将其附加到电子邮件中.
我不知道如何实现这一目标.
# views.py
context = {
'products_dict': products_dict,
'main_categories': main_categories,
'user_category': user_category
}
response = PDFTemplateResponse(request=request,
context=context,
template="my_template.html",
filename="filename.pdf",
show_content_in_browser=True,
cmd_options={'encoding': 'utf8',
'quiet': True,
'orientation': 'landscape',
}
)
return response
Run Code Online (Sandbox Code Playgroud)
上面的代码生成了我想要的PDF.问题是我不想在浏览器中显示PDF或开始下载(我不想返回响应).我只是想创建它,然后将文件附加到这样的电子邮件:
email = EmailMessage()
email.subject = "subject"
email.body = "Your PDF"
email.from_email = "sender@gmail.com"
email.to = [ "receiver@gmail.com", ]
# Attach PDF file to the email
email.attach_file(my_pdf_file_here)
# Send email
email.send()
Run Code Online (Sandbox Code Playgroud)
我尝试使用子进程,但似乎我不能在生成PDF之前将上下文发送到我的模板来呈现它.
编辑(解决方案):感谢丹尼尔罗斯曼帮助实现我的目标.我在这里使用了wkhtmltopdf的测试文件:http://pydoc.net/Python/django-wkhtmltopdf/1.1/wkhtmltopdf.tests.tests/
这就是我在观点中所做的:
order = Order.objects.get(id=order_id)
return_file = "tmp/" + 'order' + str(order_id) + …
Run Code Online (Sandbox Code Playgroud) 如果我在会话中有值并且我需要在会话中获取所有值,例如
String[] name = request.getParameterValues("values");
HttpSession session = request.getSession();
for(String temp:name)
{
if(temp.equalsIgnoreCase("a"))
{
session.setAttribute("a", temp);
out.println("a is Running<br>");
}
if(temp.equalsIgnoreCase("b"))
{
session.setAttribute("b", temp);
out.println("b is Running<br>");
}
if(temp.equalsIgnoreCase("c"))
{
session.setAttribute("c", temp);
out.println("c is Running<br>");
}
if(temp.equalsIgnoreCase("d"))
{
session.setAttribute("d", temp);
out.println("d is Running<br>");
}
if(temp.equalsIgnoreCase("e"))
{
session.setAttribute("e", temp);
out.println("e is Running<br>");
}
if(temp.equalsIgnoreCase("f"))
{
session.setAttribute("f", temp);
out.println("f is Running<br>");
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个包含100,000个文件的目录,我需要迭代它们以读取值.现在我listFiles()
用来加载数组中的所有文件,然后逐个迭代.但是有没有一种内存有效的方法可以在不加载数组的情况下执行此操作?
File[] tFiles = new File(Dir).listFiles();
try {
for (final File tFile : tFiles) {
//Process files one by one
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试加快我的zsh主题提示,并且hg
检查比它git
的速度慢一点.这导致在使用hg
插件时显示较慢的提示.
因为git
我们使用git rev-parse --is-inside-work-tree
哪个是标准且非常快.
因为hg
我试图找到一个类似的命令,但没有什么是快的:
hg --cwd $PWD root
hg summary
hg root
hg -q stat
所有这些命令都有效,但效率不高git rev-parse --is-inside-work-tree
.
还尝试了这个目前最好的小脚本.我还能做些什么来加快这个脚本的速度?
is_dir_hg() {
local root="$(pwd -P)"
while [[ $root && ! -d $root/.hg ]]
do
root="${root%/*}"
done
echo "$root"
}
Run Code Online (Sandbox Code Playgroud)
请注意,hg
即使我们不在根目录中,解决方案也必须检测目录.
我在加拿大,我们的应用正在发布不同的短信。例如,我们可以向用户发送测试短信,针对不同事件发送短信提醒,等等。
我从此列表中看到加拿大不支持发件人ID:https : //docs.aws.amazon.com/sns/latest/dg/sms_supported-countries.html。因此,结果是从一个随机数发送了SMS。
有没有办法告诉AWS SNS始终从相同的号码发送我们的SMS?
希望你能在这里看到我想要的东西.显然这段代码不起作用,但我基本上试图说,如果随机数是这些值中的一个,运行此代码.如果随机数是运行该代码的另一个值.我需要的东西相当于一个大的或声明而不使用大的if-else.谢谢
static int cardNumber = rnd.nextInt(13) + 1;
if (cardNumber == 1||11||12||13)
{
System.out.println(faceCard + " of " + suit);
}
else
{
System.out.println(cardNumber + " of " + suit);
}
Run Code Online (Sandbox Code Playgroud)