简体   繁体   中英

ValueError with python and django

I have what I am sure is a very simple error in my django/python code. Essentially I'm trying (for now) to simply make a html-form that has a few drop-down lists and a file-upload capability. I have the following snippets of code:

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:

{% 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:

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/
need more than 1 value to unpack
Request Method: GET
Django Version: 1.5.2
Exception Type: ValueError
Exception Value:
need more than 1 value to unpack

Error during template rendering
In template /path/to/templates/convert.html, error at line 16

but I fail to understand why. I'm a newbie with django, but sort of used to python. Line 16 in convert.html is the line that has the

{{ 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.

As per the documentation :

choices

An iterable (eg, a list or tuple) of 2-tuples to use as choices for this field. This argument accepts the same formats as the choices argument to a model field. 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. You might want to consider:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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