Python django不带模型上传文件
我想上传没有模型的文件(文本/图像)(仅使用视图和模板)。理想情况下,我想阅读或写作的位置 我目前的代码如下: 在我的模板/foo/help/UploadFileContent.html中Python django不带模型上传文件,python,django,Python,Django,我想上传没有模型的文件(文本/图像)(仅使用视图和模板)。理想情况下,我想阅读或写作的位置 我目前的代码如下: 在我的模板/foo/help/UploadFileContent.html中 <!doctype html> <html> <body> <link rel="stylesheet" href="{{STATIC_URL}}/stylesheets/jquery-ui.css"> <div class="main-content"&
<!doctype html>
<html>
<body>
<link rel="stylesheet" href="{{STATIC_URL}}/stylesheets/jquery-ui.css">
<div class="main-content">
<div class="container">
<div class="container">
<div class="row">
<div class="box">
<div class="box-content">
<div class="col-md-12">
<form method="post" id="fileupload" action="/helpsubmitfilecontent/" accept-charset="utf-8" class="fill-up">
{% csrf_token %}
<div class="row">
<div class="col-lg-4">
<ul class="padded separate-sections">
<li>
<input type="text" name="name" id="name" placeholder="Name"/>
</li>
<li>
<textarea name="content" id="content"
placeholder="Help Contents" style="height: 250px;width: 700px"></textarea>
</li>
<li>
<input type="file" name="myfile" id="myfile" />
</li>
</ul>
<div class="form-actions">
<button type="submit" class="btn btn-blue">Submit</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
$('#fileupload').submit(function(evnt){
if($('#name').val().length <=0){
$('#name').attr('style','border-color:red');
evnt.preventDefault();
}
if($('#content').val().length <=0){
$('#content').attr('style','border-color:red');
evnt.preventDefault();
}
});
</script>
</html>
操作文件数据时出现以下错误
Exception Value:
"Key 'myfile' not found in <MultiValueDict: {}>"
异常值:
“在中找不到密钥'myfile'”
请给我一些建议。理想情况下,我不想使用任何数据库,因为我只想将文本/图像导入到静态位置。上传文件时不必使用模型。你可以用这个
<form method="post" enctype="multipart/form-data">
<input type="file" name="sentFile" />
<input type="submit" name="submit" value="Upload" />
</form>
您的
标记中缺少enctype=“multipart/form data”
属性
编辑
handle\u uploaded\u file
(您称之为f
)的参数是UploadedFile的一个实例,它根据具有name
属性,因此您可以根据名称更改目的地。或者,您可以查看内容本身并以这种方式确定。如何将文件保存到句柄中的/opt/tmp/foo\u上载的文件(f),不想播放设置。pyI当前假定用户输入是文本文件,但它可能是图像,我想将其存储到/user/img1。{png/bmp/jpeg/*}我们如何将文件保存到句柄中的/opt/tmp/foo_上传的_文件(f),不想使用settings.py播放
<form method="post" enctype="multipart/form-data">
<input type="file" name="sentFile" />
<input type="submit" name="submit" value="Upload" />
</form>
def myview(request):
f = request.FILES['sentFile'] # here you get the files needed
print f.name