如何将weasyprint生成的PDF文件保存(存储)到指定目录?

UMA*_*MAR 6 python django weasyprint

我需要将weasyprint生成的pdf文件作为某个指定的文件夹,例如在文件夹“ my_project/pdf_files/ ”中

注意:我正在为我的项目使用 django 框架。这是项目的结构。

my_project/

----pdf_generator_app/

--------admin.py

--------models.py

--------views.py

pdf_files/
Run Code Online (Sandbox Code Playgroud)

目前我在我的views.py 中有这个

def generate(student_id):
    student = get_object_or_404(Student, application_id=student_id)
    html = render_to_string('contract.html', {'student': student})
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'filename="some_file.pdf"'
    weasyprint.HTML(string=html).write_pdf('myfile.pdf', 
                                           presentational_hints=True,
                                           stylesheets=[weasyprint.CSS(settings.STATIC_ROOT + '/css/styles.css')])
    return response
Run Code Online (Sandbox Code Playgroud)

但是此视图不会将 pdf 文件存储到目录中。它只是在浏览器上显示文件。我需要将文件存储在目录my_project/pdf_files/