我一直试图上传一个简单的文本文件几个小时,但我似乎仍然无法让它工作.
我一直收到无效的表格,说我错过了"file_source".
为什么"file_source"没有发布?
我也让它实际发送"file_source",但它仍然说它丢失了.应该为Django FileFiled提供什么类型的元素?
Django表格:
class FileUploadForm(forms.Form):
file_source = forms.FileField()
Run Code Online (Sandbox Code Playgroud)
Django模板(渲染表格):
<form action="/upload/" method="post" id="file-upload-form" enctype="multipart/form-data"> {% csrf_token %}
{{ form }}
<button type="submit" class="btn btn-primary" id='upload-btn'>Upload</button>
</form>
Run Code Online (Sandbox Code Playgroud)
JQuery/Ajax上传:
function uploadFile() {
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action')
});
return false;
}
$(function() {
$('#file-upload-form').submit(uploadFile);
});
Run Code Online (Sandbox Code Playgroud)
Django View哪收到POST:
def upload_view(request):
if request.is_ajax():
form = FileUploadForm(request.POST)
if form.is_valid():
print 'valid form'
else:
print 'invalid form'
print form.errors
return HttpResponseRedirect('/ingest/')
Run Code Online (Sandbox Code Playgroud) 我按照Qt提供的Spin Box Delegate教程,尝试实现自己的QItemDelegate
.它将用于指定a QComboBox
来表示QTableView
单元格中的数据,但它不起作用.
我最大的问题是我不知道什么时候QItemDelegate
会被利用.
何时itemModel->setData()
使用或何时使用itemModel->setItem()
.我怀疑setItem()
是因为我重新实现了QItemDelegate
(强调"项目"),但教程使用setData()
并且它工作正常.
我知道,如果指定QItemDelegate
不起作用,它使用默认值,但我现在怎么说我指定的那个不起作用?
什么时候我应该怀疑QTableView
使用我的代表.我想指定每个单元格使用哪些代理.这是可能的还是QTableView
唯一使用一个代表?
如何将我指定的项目来填充QComboBox
一旦获得通过所显示的QTableView
?
我QItemDelegate
在这里实现:
QComboBox
位于mainwindow.cpp中的注释"Enabled"下面.qcomboboxitemdelegate.h
#ifndef QCOMBOBOXITEMDELEGATE_H
#define QCOMBOBOXITEMDELEGATE_H
#include <QItemDelegate>
#include <QComboBox>
class QComboBoxItemDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit QComboBoxItemDelegate(QObject *parent = 0);
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index);
void setEditorData(QWidget *editor, const QModelIndex &index);
void …
Run Code Online (Sandbox Code Playgroud) 我正在使用 DjangoRestFramework 来制作 API。目前我有 OAuth2 身份验证工作,这意味着我可以生成一个有效的访问令牌来使用 API。
如何上传用户文件?我需要上传一个文件并将其与上传它的用户相关联。
我目前正在尝试这样做
api/views.py:
class FileUploadView(APIView):
parser_classes = (FileUploadParser,)
def put(self, request, filename, format=None):
file_obj = request.FILES['file']
# do stuff
return Response(status=204)
Run Code Online (Sandbox Code Playgroud)
api/urls.py 包含这一行:
url(r'^files/', 'api.views.FileUploadView'),
Run Code Online (Sandbox Code Playgroud)
但是当我尝试上传文件时,我收到一条错误消息:
'CSRF verification failed. Request aborted'
'Reason given for failure: CSRF cookie not set'
Run Code Online (Sandbox Code Playgroud)
当我尝试这个 curl 命令时:
curl -XPUT http://localhost:8000/files/ -H 'Authorization: Bearer some_access_token' -F filedata=@localfile.txt
Run Code Online (Sandbox Code Playgroud)
这是我的 REST_FRAMEWORK 默认值:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.OAuth2Authentication',
)
}
Run Code Online (Sandbox Code Playgroud) 我有一个Board
(aka &mut Vec<Vec<Cell>>
)我想迭代它时更新.我想要更新的新值来自一个函数,它需要一个&Vec<Vec<Cell>>
我正在更新的集合.
我尝试了几件事:
使用board.iter_mut().enumerate()
,row.iter_mut().enumerate()
以便我可以cell
在最里面的循环中更新.Rust不允许调用该next_gen
函数,因为它需要a,&Vec<Vec<Cell>>
并且当您已经有一个可变引用时,您不能拥有不可变引用.
更改next_gen
功能签名以接受a &mut Vec<Vec<Cell>>
.Rust不允许对对象进行多次可变引用.
我目前正在将所有更新推迟到a HashMap
,然后在我执行迭代后应用它们:
fn step(board: &mut Board) {
let mut cells_to_update: HashMap<(usize, usize), Cell> = HashMap::new();
for (row_index, row) in board.iter().enumerate() {
for (column_index, cell) in row.iter().enumerate() {
let cell_next = next_gen((row_index, column_index), &board);
if *cell != cell_next {
cells_to_update.insert((row_index, column_index), cell_next);
}
}
}
println!("To Update: {:?}", cells_to_update);
for ((row_index, column_index), cell) in …
Run Code Online (Sandbox Code Playgroud) 我是OpenMP的新手。我正在尝试将多个内核用于for循环,但出现此编译错误:
“错误C3016:'x':OpenMP'for'语句中的索引变量必须具有带符号的整数类型”。
我知道OpenMP 2.0是Microsoft Visual Studio 2010(我正在使用的标准)的标准组件,由于某种原因它不支持未签名的数据类型,但是我不相信在此循环中使用任何未签名的数据类型。
这是我的代码的样子:
可变小数:
struct Vertex
{
float x, y;
};
FractalGenerator::Vertex FractalGenerator::_fracStart;
FractalGenerator::Vertex FractalGenerator::_fracEnd;
float FractalGenerator::_pointPrecision = 0.008f;
float FractalGenerator::_scaleFactor = 1;
Run Code Online (Sandbox Code Playgroud)
对于循环:
float _fracStep = _pointPrecision / _scaleFactor;
#pragma omp parallel for
for (float x = _fracStart.x; x < _fracEnd.x; x += _fracStep)
{
for (float y = _fracStart.y; y < _fracEnd.y; y += _fracStep)
{
Run Code Online (Sandbox Code Playgroud) 我正在尝试在django模板中设置maxlength html属性,但是当我尝试使用{{field.max_length}}获取我在模型中指定的值时,不会显示任何内容.我可以获得任何其他字段值.
相关代码段:
{% for field in form %}
{% if "_color" in field.name %}
<div class="control-group">
<label class="control-label">{{field.label}}</label>
<input type="text" ng-init="{{field.name}}='{{field.value}}'" ng-model="{{field.name}}" ng-change="{{field.name}}_picker={{field.name}}" placeholder="{{field.label}}" maxlength="{{field.max_length}}">
Run Code Online (Sandbox Code Playgroud) 我试图在 Scrapy 的帮助下抓取一个非常简单的网页,它是 xpath 选择器,但由于某种原因,我拥有的选择器在 Scrapy 中不起作用,但它们在其他 xpath 实用程序中起作用
我正在尝试解析这段 html:
<select id="chapterMenu" name="chapterMenu">
<option value="/111-3640-1/20th-century-boys/chapter-1.html" selected="selected">Chapter 1: Friend</option>
<option value="/111-3641-1/20th-century-boys/chapter-2.html">Chapter 2: Karaoke</option>
<option value="/111-3642-1/20th-century-boys/chapter-3.html">Chapter 3: The Boy Who Bought a Guitar</option>
<option value="/111-3643-1/20th-century-boys/chapter-4.html">Chapter 4: Snot Towel</option>
<option value="/111-3644-1/20th-century-boys/chapter-5.html">Chapter 5: Night of the Science Room</option>
</select>
Run Code Online (Sandbox Code Playgroud)
Scrapy parse_item 代码:
def parse_item(self, response):
itemLoader = XPathItemLoader(item=MangaItem(), response=response)
itemLoader.add_xpath('chapter', '//select[@id="chapterMenu"]/option[@selected="selected"]/text()')
return itemLoader.load_item()
Run Code Online (Sandbox Code Playgroud)
Scrapy 不会从中提取任何文本,但是如果我得到相同的 xpath 和 html 片段并在此处运行它,它就可以正常工作。
如果我使用这个 xpath:
//select[@id="chapterMenu"]
Run Code Online (Sandbox Code Playgroud)
我得到了正确的元素,但是当我尝试访问里面的选项时,它什么也没得到
我有这个非常简单的代码(scala-2.10):
import scala.io.Source
object Test2 {
def main(args: Array[String]): Unit = {
for(line <- Source.fromFile("/Users/alexei/words.txt", "utf-8").getLines()) {
println(line)
}
}
}
Run Code Online (Sandbox Code Playgroud)
编译时我收到此错误消息:
Test2.scala:3: error: ';' expected but 'object' found.
object Test2 {
^
one error found
Run Code Online (Sandbox Code Playgroud)
我非常困惑何时使用分号。我有其他与此类似的代码,并且在没有任何分号的情况下编译没有任何问题。
有人可以解释这个特定的错误并详细说明需要分号的所有情况吗?