Django TemplateSyntaxError: too many values to unpack


December 2018


3.3k time


I'm working with a django form, and I have a choice field. I think the problem may be that the choices are fetched dynamically, and right now there's only one value. I'm getting the TemplateSyntaxError: too many values to unpack. Some of the other posts seem to say that having only one value is a problem, so i adjusted my function that fetches the choices, and changed it so it added to blank options at the beginning, just as a test. However this brought up another error: need more than 0 values to unpack

Not really sure what to do about this, because even if there is only one value, I need it to still execute.


class UploadFileForm(forms.Form):
    category = forms.ChoiceField(get_category_list())
    file = forms.FileField()

Category Fetch Function:

def get_category_list():
    cats = [(), ()]
    for i in os.listdir(settings.MEDIA_ROOT + '/forms'):
    return cats

Template Section:

<div id='addformdialog' title='Add Form'>
    {{ form.as_p }}


def fm(request):
    if request.session['SecurityLevel'] != 2:
        return HttpResponse('Access Denied!')

    if request.method == 'POST':
        form = UpoadFileForm(request.POST, request.FILES)
        if form.is_valid():
            destination = open(settings.MEDIA_ROOT + "/forms/" + request.POST['category'] + "/" + request.FILES['file'].name, 'wb+')
            for chunk in request.FILES['file'].chunks():
            form = UploadFileForm()
            return render_to_response('admin/fm.html', {'categories':cats, 'form':form, 'uploadsuccess':True})
        cats = get_category_list()
        form = UploadFileForm()
        return render_to_response('admin/fm.html', {'categories':cats, 'form':form})

1 answers


choicesПредполагается, что итератор 2-кортежей. Вы только присоединяя одну строку, которая вызывает хаос из-за того, как строки и кортежи взаимодействуют (я дам вам информацию, если вы действительно заботитесь). Добавление 2-кортежи вместо этого.