python - 通过 tweepy 从 django 表单将图像上传到 Twitter

标签 python django twitter tweepy

我想将 django 表单拍摄的图像上传到 Twitter:

<div class="panel panel-danger">
                <div class="panel-heading">
                    <h3 class="panel-title">Panel title</h3>
                </div>
                <div class="panel-body">
                    <form action="" method="POST" role="form" enctype="multipart/form-data">
                        {% csrf_token %}
                        <legend>Upload a file: </legend>

                        <div class="form-group">
                            <input type="file" name="file" class="form-control" id="">
                        </div>



                        <button type="submit" class="btn btn-primary">Submit</button>
                    </form>
                </div>
            </div>

我得到的图像是:

if fileForm.is_valid():
            print "file is uploaded."
            paramFile = open(request.FILES['file'],'rb')
            # paramFile = request.FILES['file'].read()
            api.update_with_media(paramFile,status="Hello first image")

我得到的错误是:

coercing to Unicode: need string or buffer, InMemoryUploadedFile found

如何将其上传到 Twitter?

最佳答案

方法 update_with_media() 只有一个采用文件名的位置参数。 所以你可以指定文件名,如下所示:

api.update_with_media(request.FILES['file'].name,
                      status="Hello first image")

您还应该使用关键字参数“file”传递文件:

api.update_with_media(request.FILES['file'].name,
                      file=request.FILES['file'], 
                      status="Hello first image")

关于python - 通过 tweepy 从 django 表单将图像上传到 Twitter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40826850/

相关文章:

python - Alembic 列类型更改会导致语法错误

django - 在 Django 的测试中连接模拟和客户端

javascript - 使用 Backbone 的 HTML5 的 Twitter 卡片历史记录

ios - 带有两个按钮的 Xcode 应用程序。当我单击其中一个时,两者的操作都会被触发。为什么会这样?

Jquery (JSON) - 如何阅读 Twitter 趋势词?

python - 在 pandas 的 groupby 语句中将两列相乘

python - Linux nm 的 Windows 模拟来查看 PYD 文件的内容?

python - 将 Pandas Dataframe 转换为嵌套 json

python - Django 有 `__not_equal` 吗?

django - 工厂男孩 : define field that depends on other field