模板渲染期间要解压缩的值太多 [英] Too many values to unpack during template rendering

查看:23
本文介绍了模板渲染期间要解压缩的值太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习 Django,但遇到了这个让我有点难住的错误.我正在尝试将我的表单放到我的主页上

我收到此错误:

代码:

home/views.py:

from django.shortcuts 导入渲染从表单导入 TestForm从 django.http 导入 HttpResponseRedirect定义主页(请求):如果请求 == 'POST':# 创建一个即时表单并用请求中的数据填充它form = TestForm(request.POST)如果 form.is_valid():# 根据需要处理form.cleaned_data中的数据form.cleaned_data()# 重定向到一个新的 URL:返回 HttpResponseRedirect('/test/')# 如果是 GET(或任何其他方法),我们将创建一个空白表单别的:表单 = TestForm()返回渲染(请求,'home/home_page.html',{'form':form})def scan_events(请求):如果请求==POST":# json = request.POST['testData']# 文件上传 ot c/p 事件的条件语句return render(request, 'home/test.html', {'data': request.POST})定义测试(请求):请求(请求,'home/test.html')

home/forms.py

from django 导入表单TEST_TYPE_CHOICES = ('HDFS', 'HIVE', 'BOTH')类 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/', 包含(admin.site.urls)),url(r'^$', 'home.views.home', name='home'),url(r'test/$', 'home.views.test'),)

home/templates/home/home_page.html

{% 扩展 'index/index.html' %}{% 加载静态文件 %}{% 块头 %}<script type="text/javascript" src="{{ STATIC_URL }}home/js/home.js" async></script><link href="{{ STATIC_URL }}home/css/home.css" rel="stylesheet">{% 端块头 %}{% 块内容 %}<div>欢迎使用 Trinity E2E 测试</div><form id="test-form" action="/test/" method="post">{# 将数据传递到/test/URL #}{% csrf_token %}{{ 形式 }}<input id="submit-test" type="submit" class="btn btn-default btn-lg" value="Submit"></表单>{% 端块含量 %}

解决方案

choices 应该是键-描述对的序列(准确地说是可迭代的).

TEST_TYPE_CHOICES = [('HDFS', 'HDFS'),('蜂巢', '蜂巢'),('BOTH', 'HDFS 和 HIVE'),]

错误信息说明:

字符串也是序列.因此,使用 choices 的代码将字符串视为 4 个字符的序列(确切地说是字符串,因为 Python 中没有字符类型).这就是为什么你会得到错误:too many values to unpack

<预><代码>>>>a, b = ('HDFS', 'HDFS')>>>a, b = 'HDFS'回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中ValueError:解包的值太多

如果字符串都是2个字符的字符串,就会隐藏(不能解决)问题.

<预><代码>>>>a, b = '高清'>>>一个'H'>>>乙'D'

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:

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 %}

解决方案

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'

这篇关于模板渲染期间要解压缩的值太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆