开发者

Google App Engine + Form Validation

开发者 https://www.devze.com 2023-02-01 07:09 出处:网络
I would like to do google app engine form validation but I dont know how to do it? I tried like this: from google.appengine.ext.db import djangoforms

I would like to do google app engine form validation but I dont know how to do it? I tried like this:

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)

And I wanted to display it like this:

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

'item1' : SurveyForm() }

And I have this error message:

Traceback (most recent call last): File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp_init_.py", line 515, in call handler.get(*groups) File "C:\Program Files\Google\google_appengine\demos\b00213576\main.py", line 144, in get self.response.out.write(template.render(path, template_values)) File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\template.py", line 143, in render return t.render(Context(template_dict)) File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\template.py", line 183, in wrap_render return orig_render(context) File "C:\Program Files\Google\google_appengine\lib\django\django\template_init_.py", line 168, in render return self.nodelist.render(context) File "C:\Program Files\Google\google_appengine\lib\django\django\template_init_.py", line 705, in render bits.append(self.render_node(node, context)) File "C:\Program Files\Google\google_appengine\lib\django\django\template_init_.py", line 718, in render_node return(node.render(context)) File "C:\Program Files\Google\google_appengine\lib\django\django\template\defaulttags.py", line 209, in render return self.nodelist_true.render(context) File "C:\Program Files\Google\google_appengine\lib\django\django\template_init_.py", line 705, in render bits.append(self.render_node(node, context)) File "C:\Program Files\Google\google_appengine\lib\django\django\template_init_.py", line 718, in render_node return(node.render(context)) File "C:\Program Files\Google\google_appengine\lib\django\django\template_init_.py", line 768, in render return self.encode_output(output) File "C:\Program Files\Google\google_appengine\lib\django\django\template_init_.py", line 757, in encode_output return str(output) File "C:\Program Files\Google\google_appengine\lib\django\django\newforms\util.py", line 26, in str return self.unicode().encode(settings.DEFAULT_CHARSET) File "C:\Program Files\Google\google_appengine\lib\django\django\newforms\forms.py", line 73, in unicode return self.as_table() File "C:\Program Files\Google\google_appengine\lib\django\django\newforms\forms.py", line 144, in as_table return self._html_output(u'%(label)s%(errors)s%(field)s%(help_text)s', u'%s', '', u'

%s', False) File "C:\Program Files\Google\google_appengine\lib\django\django\newforms\forms.py", line 129, in _html_output output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text}) File "C:\Program Files\Google\google_appengine\lib\django\django\newforms\forms.py", line 232, in unicode value = value.str() File "C:\Program Files\Google\google_appengine\lib\django\django\newforms\util.py", line 26, in str return self.unicode().encode(settings.DEFAULT_CHARSET) File "C:\Program Files\Google\google_appengine\lib\django\django\newforms\widgets.py", line 246, in unicode return u'

    \n%s\n
' % u'\n'.join([u'
  • %s
  • ' % w for w in self]) File "C:\Program Files\Google\google_appengine\lib\django\django\newforms\widgets.py", line 238, in iter yield RadioInput(self.name, self.value, self.attrs.copy(), choice, i) File "C:\Program Files\Google\google_appengine\lib\django\django\newforms\widgets.py", line 212, in init self.choice_value = smart_unicode(choice[0]) TypeError: 'int' object is unsubscriptable

    Do You have any idea how I can do this validation in different case?

    I have tried to do it using this kind of:

    class ItemUserAnswer(djangoforms.ModelForm):
    

    class Meta: model = UserAnswer

    But I dont know how to add extra labels to this form and it is displayed in one line. Do You have any suggestions?

    Thanks a lot as it making me crazy why it is still not working:/


    The builtin method is_valid() should validate. When submitting a form I do it with 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())
    


    Here is the issue

    rating = forms.ChoiceField(choices=range(1,6), label='What is your occupation?', widget=forms.RadioSelect)

    choices is set to an integer value.

    0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消