我想做谷歌应用程序引擎表单验证,但我不知道如何做?我试过这样做:
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() }
我有一条错误信息:
文件“C:\ Files\Google\google_appengine\google\appengine\ext\webapp_init_.py",Files\Google\google_appengine\demos\b00213576\main.py",第515行,在 call handler.get(*groups) File "C:\Program Files\Google\google_appengine\demos\b00213576\main.py”,第144行中,在get self.response.out.write(template.render(路径),)文件“C:\ Files\Google\google_appengine\google\appengine\ext\webapp\template.py",Files\Google\google_appengine\google\appengine\ext\webapp\template.py",第143行,在呈现返回t.render(上下文(Template_dict))文件"C:\Program第183行,在wrap_render返回orig_render(上下文)文件中"C:\Program Files\Google\google_appengine\lib\django\django\template_init_.py",第168行,在呈现返回(上下文)文件“C:\ Files\Google\google_appengine\lib\django\django\template_init_.py",Files\Google\google_appengine\lib\django\django\template_init_.py",第705行,在呈现bits.append(节点,上下文)文件"C:\Program Files\Google\google_appengine\lib\django\django\template_init_.py”,第718行中,在Files\Google\google_appengine\lib\django\django\template\defaulttags.py",返回(node.render(上下文))文件"C:\Program第209行中,在呈现返回self.nodelist_true.render(上下文)文件“C:\ render_node node.render第705行中,在呈现bits.append(self.render_node(节点),(上下文)文件“C:\ Files\Google\google_appengine\lib\django\django\template_init_.py",Files\Google\google_appengine\lib\django\django\template_init_.py",第718行,在render_node返回(node.render(上下文))文件"C:\Program Files\Google\google_appengine\lib\django\django\template_init_.py",第768行,在呈现返回self.encode_output(输出)文件”C:\Files\Google\google_appengine\lib\django\django\template_init_.py“,第757行,在返回str(输出)文件“C:\ Files\Google\google_appengine\lib\django\django\newforms\util.py",self.unicode().encode(settings.DEFAULT_CHARSET)第26行,在str返回Files\Google\google_appengine\lib\django\django\newforms\forms.py",文件”C:\Files\Google\google_appengine\lib\django\django\newforms\forms.py“,第73行中,在Files\Google\google_appengine\lib\django\django\newforms\forms.py",unicode中,返回self.as_table()文件"C:\Program 第144行,在as_table中返回‘,'',u’‘
%s',False)文件“C:\ Files\Google\google_appengine\lib\django\django\newforms\forms.py",Files\Google\google_appengine\lib\django\django\newforms\forms.py",第129行,在_html_output output.append中(normal_row%{‘错误’:bf_errors,'label':label,'field':unicode(bf),'help_text':help_text})文件"C:\Program Files\Google\google_appengine\lib\django\django\newforms\forms.py”,第232行,在Files\Google\google_appengine\lib\django\django\newforms\util.py",unicode中,值= value.str() File "C:\Program 第26行,在str返回文件"C:\Program Files\Google\google_appengine\lib\django\django\newforms\widgets.py",第246行,在unicode返回u‘
加入(u‘%s)
‘%w for w in self)文件“C:\ Files\Google\google_appengine\lib\django\django\newforms\widgets.py",Files\Google\google_appengine\lib\django\django\newforms\widgets.py",第238行,iter RadioInput(self.name,self.value,self.attrs.copy(),self.name,i) File "C:\Program Files\Google\google_appengine\lib\django\django\newforms\widgets.py”,line 212,在init中, self.choice_value =smart_unicode(选择) TypeError:'int‘对象不可订阅
你知道我如何在不同的情况下进行这种验证吗?
我试过用这样的方法:
class ItemUserAnswer(djangoforms.ModelForm):类Meta: model = UserAnswer
但我不知道如何添加额外的标签到这个表单,它显示在一行。你有什么意见建议?
非常感谢,因为它使我疯狂,为什么它仍然不工作:/
发布于 2010-12-22 23:49:25
内置方法is_valid()应该验证。在提交表单时,我使用HTTP完成
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-22 17:48:30
以下是问题所在
评级= forms.ChoiceField(choices=range(1,6),label=“您的职业是什么?”,widget=forms.RadioSelect)
选项设置为整数值。
https://stackoverflow.com/questions/4511529
复制相似问题