Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Too many values to unpack during template rendering

I am learning Django right now and I came across this error which I am a bit stumped on. I am trying to get my form onto the my homepage

I get this error:

enter image description here

code:

home/views.py:

from django.shortcuts import render
from forms import TestForm
from django.http import HttpResponseRedirect

def home(request):
    if request == 'POST':
        # create a form instane and populate it with data from the request
        form = TestForm(request.POST)
        if form.is_valid():
            # process the data in form.cleaned_data as required
            form.cleaned_data()
            # redirect to a new URL:
            return HttpResponseRedirect('/test/')
    # if a GET (or any other method) we'll create a blank form
    else:
        form = TestForm()
    return render(request, 'home/home_page.html', {'form': form})


def scan_events(request):
    if request == "POST":
        # json = request.POST['testData']
        # condition statement for file upload ot c/p events

        return render(request, 'home/test.html', {'data': request.POST})


def test(request):
    request(request, 'home/test.html')

home/forms.py

from django import forms

TEST_TYPE_CHOICES = ('HDFS', 'HIVE', 'BOTH')

class TestForm(forms.Form):
    # hdfs_test = forms.MultipleChoiceField()
    # hive_test = forms.MultipleChoiceField()
    # hdfs_hive_test = forms.MultipleChoiceField()
    test_type = forms.MultipleChoiceField(required=True, widget=forms.RadioSelect(), choices=TEST_TYPE_CHOICES)
    event_textarea = forms.Textarea(attrs={'rows': '8', 'class': 'form-control', 'placeholder': 'Events...', 'id': 'event_textarea'})
    # file_upload = forms.FileInput()

urls.py:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'home.views.home', name='home'),
    url(r'test/$', 'home.views.test'),

)

home/templates/home/home_page.html

{% extends 'index/index.html' %}

{% load staticfiles %}
{% block head %}
  <script type="text/javascript" src="{{ STATIC_URL }}home/js/home.js" async></script>
  <link href="{{ STATIC_URL }}home/css/home.css" rel="stylesheet">
{% endblock head %}

{% block content %}

  <div>Welcome to Trinity E2E testing</div>

  <form id="test-form" action="/test/" method="post"> {# pass data to /test/ URL #}
    {% csrf_token %}

    {{ form }}

    <input id="submit-test" type="submit" class="btn btn-default btn-lg" value="Submit">
  </form>

{% endblock content %}
like image 396
Liondancer Avatar asked Jan 09 '23 04:01

Liondancer


1 Answers

choices should be a sequence (iterable to be exact) of key-description pairs.

TEST_TYPE_CHOICES = [
    ('HDFS', 'HDFS'),
    ('HIVE', 'HIVE'),
    ('BOTH', 'Both of HDFS and HIVE'),
]

Explanation of the error message:

Strings are also sequences. So the code that use choices see the string as a sequence of 4 character (string to be exact, because there's no character type in Python). That's why you get the error: too many values to unpack

>>> a, b = ('HDFS', 'HDFS')
>>> a, b = 'HDFS'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack

If the strings were all 2-character strings, it would hide (not solve) the problem.

>>> a, b = 'HD'
>>> a
'H'
>>> b
'D'
like image 170
falsetru Avatar answered Jan 16 '23 21:01

falsetru