简体   繁体   English

使用python和django的ValueError

[英]ValueError with python and django

I have what I am sure is a very simple error in my django/python code. 我的django / python代码中有一个非常简单的错误。 Essentially I'm trying (for now) to simply make a html-form that has a few drop-down lists and a file-upload capability. 基本上我正在尝试(现在)简单地制作一个具有一些下拉列表和文件上传功能的html表单。 I have the following snippets of code: 我有以下代码片段:

views.py: views.py:

def convert(request):
if request.POST:
    form = ConvertForm(request.POST,request.FILES)

    if form.is_valid():
        form.save()

        # Change this to some result page,                                                   
        # but for now, just see that we got the file                                         
        return HttpResponseRedirect('/convert/convert')
else:
    form = ConvertForm()

args = {}
args.update(csrf(request))
args['form']=form

return render_to_response('convert.html',args)

in convert.html: 在convert.html中:

{% block content %}  
 <form action="/convert/convert/" method="post" enctype="multipart/form-data">{% csrf_token %}  
 <ul> 
  {{ form.as_ul }}  
 </ul> 
 <input type="submit" name="submit" value="Convert"> 
 </form>   
{% endblock %}  

and in my forms.py: 在我的forms.py中:

from django import forms

class ConvertForm(forms.Form):
    ff_from = forms.ChoiceField(choices=('a'))
    ff_to = forms.ChoiceField(choices=('b'))
    file = forms.FileField(max_length=200)

The error I'm getting is the following: 我得到的错误如下:

ValueError at /convert/convert/ ValueError at / convert / convert /
need more than 1 value to unpack 需要超过1个值来解压缩
Request Method: GET 请求方法:GET
Django Version: 1.5.2 Django版本:1.5.2
Exception Type: ValueError 异常类型:ValueError
Exception Value: 例外价值:
need more than 1 value to unpack 需要超过1个值来解压缩

Error during template rendering 模板渲染时出错
In template /path/to/templates/convert.html, error at line 16 在模板/path/to/templates/convert.html中,第16行出错

but I fail to understand why. 但我不明白为什么。 I'm a newbie with django, but sort of used to python. 我是django的新手,但有点习惯蟒蛇。 Line 16 in convert.html is the line that has the convert.html中的第16行是具有的行

{{ form.as_ul }} 

piece of code. 一段代码。

Right now I'm only trying to get the form to show on my website, much less than making it do something! 现在我只是试图让我的网站上显示的表单,远远不是让它做某事!

Let me know if this description is complete, not used to posting questions here! 如果此描述完整,请告诉我,不要在此处发布问题! Thanks! 谢谢!

Your argument to choices is incorrect in the ChoiceField 's. 你在ChoiceFieldchoices论证是不正确的。

As per the documentation : 根据文件

choices

An iterable (eg, a list or tuple) of 2-tuples to use as choices for this field. 2元组可迭代 (例如,列表或元组),用作此字段的选项。 This argument accepts the same formats as the choices argument to a model field. 此参数接受与模型字段的choices参数相同的格式。 See the model field reference documentation on choices for more details. 有关详细信息,请参阅有关选项的模型字段参考文档。

In your case, you have a single element. 在您的情况下,您有一个元素。 You would at least need: 你至少需要:

choices=[('a_code', 'A Pretty Display Value')]

Mind: 心神:

  • The list 名单
  • The two elements in the tuple 元组中的两个元素

Of course, a ChoiceField with a single choice isn't really a choice. 当然, ChoiceField一个选择的ChoiceField并不是真正的选择。 You might want to consider: 您可能需要考虑:

[
    ('a_code', u'A Pretty Display Value'), 
    ('another_code', 'Another pretty display value')
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM