关于ZAKER 融媒体解决方案 合作 加入

模板渲染期间解包的值太多

CocoaChina 10-14

我现在正在学习 Django, 遇到了这个错误 , 我对此有些困惑 . 我正在尝试将表格添加到我的主页上

我收到此错误:

码:

家用 / views.py:

from django.shortcuts import renderfrom forms import TestFormfrom django.http import HttpResponseRedirectdef 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' )

家用 / forms.py

from django import formsTEST_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_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应该是键描述对的序列 ( 准确地说是可迭代的 ) .

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

错误消息的说明:

字符串也是序列 . 因此 , 使用选择的代码将字符串视为 4 个字符的序列 ( 确切地说是字符串 , 因为 Python 中没有字符类型 ) . 这就是为什么会出现错误:太多值无法解包的原因

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

如果所有字符串都是 2 个字符的字符串 , 它将隐藏 ( 但不能解决 ) 问题 .

>>> a, b = 'HD'>>> a'H'>>> b'D'

以上内容由"CocoaChina"上传发布 查看原文
相关标签 模板line

觉得文章不错,微信扫描分享好友

扫码分享