【发布时间】:2023-03-19 07:23:02
【问题描述】:
所以,我有一个问题,我找不到解决方案。我正在使用 Django 开发一个应用程序,我的前端必须在 angular-js 中。现在我可以渲染表单并发布表单中的数据,但我不知道如何使用这些表单上传文件。
这是我的代码:
在 urls.py 中
url(r'^getter/$', TemplateView.as_view(template_name = "upload.html"))
url(r'^getter/test/', views.test, name = "thanks.html")
在views.py中
def test(request):
upload_form = uploadform(request.POST, request.FILES)
data = json.loads(request.body)
file_path = data.path
在forms.py中
select_file = forms.FileField(label = "Choose File")
在我的控制器内的 js 文件中
myapp.controller('abc', function ($scope, $http)
$scope.submit = function(){
var file = document.getElementById('id_select_file').value
var json = {file : string(file)}
$http.post('test/',json)
...success fn....
...error fn...
}; });
现在的问题是,如果在我看来,如果我这样做了
f = request.FILES['select_file']
我在 MultiValueDict 中找不到错误“select_file”:{}
问题可能是发送我的帖子请求的方式并没有发送所有元数据....请帮助我解决这个问题,我花了一整天时间寻找解决方案,但无济于事。
PS:对于某些限制政策,我不能使用 Djangular,所以请给我不使用 djangular 的解决方案。谢谢
编辑:**将文件属性应用于服务器正在接收的 json 也不起作用**
【问题讨论】:
-
不知道 Django,但是你的 json 使用
file并且你的视图引用select_file这是正确的吗? -
@craigb 是的,刚刚编辑了我的问题。当我使用角度时,基本上没有 od django 的文件方法或函数起作用
标签: python django angularjs django-forms django-uploads