3

我想做谷歌应用引擎表单验证,但我不知道怎么做?我试过这样:

from google.appengine.ext.db import djangoforms
from django import newforms as forms


class SurveyForm(forms.Form):
    occupations_choices = (
    ('1', ""),
    ('2', "Undergraduate student"),
    ('3', "Postgraduate student (MSc)"),
    ('4', "Postgraduate student (PhD)"),
    ('5', "Lab assistant"),
    ('6', "Technician"),
    ('7', "Lecturer"),
    ('8', "Other" )
    )

howreach_choices = (
    ('1', ""),        
    ('2', "Typed the URL directly"),
    ('3', "Site is bookmarked"),
    ('4', "A search engine"),
    ('5', "A link from another site"),
    ('6', "From a book"),
    ('7', "Other")
    )

boxes_choices = (
    ("des", "Website Design"),
    ("svr", "Web Server Administration"),
    ("com", "Electronic Commerce"),
    ("mkt", "Web Marketing/Advertising"),
    ("edu", "Web-Related Education")
    )

name = forms.CharField(label='Name', max_length=100, required=True)
email = forms.EmailField(label='Your Email Address:')
occupations = forms.ChoiceField(choices=occupations_choices, label='What is your occupation?')
howreach = forms.ChoiceField(choices=howreach_choices, label='How did you reach this site?')
# radio buttons 1-5
rating = forms.ChoiceField(choices=range(1,6), label='What is your occupation?', widget=forms.RadioSelect)

boxes = forms.ChoiceField(choices=boxes_choices, label='Are you involved in any of the following? (check all that apply):', widget=forms.CheckboxInput)
comment = forms.CharField(widget=forms.Textarea, required=False)

我想像这样显示它:

template_values = {
        'url' : url,
        'url_linktext' : url_linktext,
        'userName' : userName,

'item1':SurveyForm() }

我有这个错误信息:

Traceback(最近一次调用最后一次):文件“C:\Program Files\Google\google_appengine\google\appengine\ext\webapp_init _.py ”,第 515 行,调用 handler.get(*groups) 文件“C:\ Program Files\Google\google_appengine\demos\b00213576\main.py",第 144 行,在获取 self.response.out.write(template.render(path, template_values)) 文件“C:\Program Files\Google\google_appengine\ google\appengine\ext\webapp\template.py”,第 143 行,在渲染中返回 t.render(Context(template_dict)) 文件“C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\template. py”,第 183 行,在 wrap_render 返回 orig_render(context) 文件“C:\Program Files\Google\google_appengine\lib\django\django\ template_init_.py”,第 168 行,在渲染中返回 self.nodelist.render(context) 文件“C:\Program Files\Google\google_appengine\lib\django\django\template_init _.py ”,第 705 行,在渲染位中.append(self.render_node(node, context)) 文件“C:\Program Files\Google\google_appengine\lib\django\django\template_init _.py ”,第 718 行,在 render_node return(node.render(context) ) 文件“C:\Program Files\Google\google_appengine\lib\django\django\template\defaulttags.py”,第 209 行,在渲染返回 self.nodelist_true.render(context) 文件“C:\Program Files\Google\ google_appengine\lib\django\django\ template_init_.py”,第 705 行,在渲染 bits.append(self.render_node(node, context)) 文件“C:\Program Files\Google\google_appengine\lib\django\django\template_init _.py ”中,第 718 行,在 render_node return(node.render(context)) 文件“C:\Program Files\Google\google_appengine\lib\django\django\template_init _.py ”,第 768 行,在 render return self.encode_output(output) 文件中“C:\Program Files\Google\google_appengine\lib\django\django\template_init _.py ”,第 757 行,在 encode_output 返回 str(输出)文件“C:\Program Files\Google\google_appengine\lib\django\ django\newforms\util.py",第 26 行,在str中 返回self.unicode第 232 行, Unicode 值 = 值。字符串
() 文件“C:\Program Files\Google\google_appengine\lib\django\django\newforms\util.py”,第 26 行,在str中 返回 self。unicode ().encode(settings.DEFAULT_CHARSET) 文件“C:\Program Files\Google\google_appengine\lib\django\django\newforms\widgets.py”,第 246 行,在unicode中 返回 u'

    \n%s\n
' % u'\n'.join([u'
  • %s
  • ' % w for w in self]) 文件“C:\Program Files\Google\google_appengine\lib\django\django\newforms\widgets.py”,第 238 行,在迭代中 产生 RadioInput(self.name, self.value, self.attrs.copy(),选择,i) 文件“C:\Program Files\Google\google_appengine\lib\django\django\newforms\widgets.py”,第 212 行,在init self.choice_value = smart_unicode(choice[ 0]) 类型错误:'int' 对象不可订阅

    您知道如何在不同情况下进行此验证吗?

    我试图用这种方法来做到这一点:

    class ItemUserAnswer(djangoforms.ModelForm):
    

    元类:模型 = UserAnswer

    但我不知道如何在此表单中添加额外的标签,并且它显示在一行中。你有什么建议吗?

    非常感谢,因为它让我疯狂,为什么它仍然无法正常工作:/

    4

    2 回答 2

    1

    内置方法is_valid()应该验证。提交表单时,我使用 HTTP POST

        def post(self):
        try:
            upload_files = self.get_uploads('file')  # 'file' is file upload field in the form
            if upload_files:
                blob_info = upload_files[0]  
        except:
            self.response.out.write('File not uploaded')        
        try:
            data = AForm(data=self.request.POST)
            if data and data.is_valid():
                # Save the data, and redirect to the view page
                entity = data.save(commit=False)
                entity.added_by = users.get_current_user()
                entity.put()
                if upload_files:            
                    im = Image(reference=entity) 
                    im.primary_image = blob_info.key()
                    im.put()
                    entity.put()
                    self.redirect('/serve/%s' % blob_info.key())
            else:
                # Reprint the form
                self.response.out.write('<html><body>'
                                        '<form method="POST" '
                                        'action="/">'
                                        '<table>')
                self.response.out.write(data)
                self.response.out.write('</table>'
                                        '<input type="submit">'
                                        '</form></body></html>')
        except:
            self.redirect('/serve/%s' % blob_info.key())
    
    于 2010-12-22T23:49:25.930 回答
    0

    这是问题

    rating = forms.ChoiceField(choices=range(1,6), label='你的职业是什么?', widget=forms.RadioSelect)

    选择设置为整数值。

    于 2010-12-22T17:48:30.940 回答