开发者

GAE + Django Forms + Validation

I have a problem with how to do validation on GAE. I have read many tutorials and I prepared django form and I wanted to validate it but I dont see eny error messages in my web page code and I dont see any errors when the values are bad.

import cgi
import os

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext.db import djangoforms
from django import newforms as forms


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

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

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

range_choice = (
    ('1', '1'),
    ('2', '2'),
    ('3', '3'),
    ('4', '4'),
    ('5', '5')
    )

name = forms.CharField(label='Enter your name', max_length=50, 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_choice , label='How would you rate the content     of this site?', widget=forms.RadioSelect)

boxes = forms.ChoiceField(choices=boxes_choices, label='Are you involved in any of the following? (check all that apply)', widget=forms.CheckboxSelectMultiple)
comment = forms.CharField(label=('Any other comments?'), widget=forms.Textarea(attrs={'cols': 40, 'rows': 10}), required=False)

class MainHandler(webapp.RequestHandler):  
def get(self):
    user = users.get_current_user()    
    if user:
        url = users.create_logout_url(self.request.uri)
        url_linktext = 'Logout'
        userName = user.nickname()

    else:
        url = users.create_login_url(self.request.uri)
        url_linktext = 'Login'
        userName = ''

    template_values = {
        'url' : url,
        'url_linktext' : url_linktext,
        'userName' : userName,
        'form' : SurveyForm(),
        }

    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

def post(self):
    user = users.get_current_user()    
    if user:
        url = users.create_logout_url(self.request.uri)
        url_linktext = 'Logout'
        userName = user.nickname()
            #self.response.out.write(index.html)

    else:
        url = users.create_login_url(self.request.uri)
        url_linktext = 'Login'
        userName = ''

    template_values = {
        'url' : url,
        'url_linktext' : url_linktext,
        'userName' : userName,
        'form' : SurveyForm(),
            }

    form = SurveyForm(self.request.POST)
    if self.request.get('submit') == 'Submit the Form':
        if form.is_valid():
           self.response.out.write("Poprawne dane")
        else:
            form = SurveyForm()
            template_values['form']=form
            path = os.path.join(os.path.dirname(__file__), 'index.html')
            self.response.out.write(template.render(path, template_values))
    else:
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values)) 

application = webapp.WSGIApplication([('/', MainHandler)],
                                     debug=True)


def main():

run_wsgi_app(application)


if __name__ == '__main__':
main()

Do You know maybe any tutorial - good tutorial how to do it? My index.html:

 <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Survey</title>
            </head>
<body>

{% if userName %}
 You are loged in like: {{ userName }}      <a href="{{ url }}">{{ url_linktext }} </a>
 <hr><br/><b>Fill in the form: </b><br/>
 <form action="/" method="Post">
 {{ form.as_p() }}
 <input type="submit" name="submit" value="Submit the Form">
 </form>
{% else %}
 Please, log in:      <a href="{{ url }}">{{ url_linktext }}</a>
{% endif %}
            </body>
            </html>

How to do it does the validation started to show me errors? And be like "normal validation"? I thought when I will use the framework it will be much more easier and faster for me but I spent 2 days and it is still not working :/ I could write my own validation in this time :/

Can You help me?


Look at {{ form.some_field.errors }}, or if you want all of them, {{ form.errors }}, or if you need non-field errors only, {{ form.non_field_errors }}.

Here is an example of how to do it: http://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-form-template

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜