值错误int()的基数为10的文字无效

时间:2013-03-21 22:10:55

标签: google-app-engine web-applications python-2.7 google-cloud-datastore jinja2

当我在我的表单中输入1933作为copyright_year时,我收到以下错误:

  

文件“C:\ programming_google_app_engine_repo \ datastore \ main.py”,行   29,在帖子中       book.copyright_year = int(self.request.get('copyright_year'))ValueError:基数为10的int()的无效文字:'1933 1655'

为什么我收到此错误?

main.py

import webapp2
import jinja2
import os

from google.appengine.ext import db

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class MainPage(webapp2.RequestHandler):
    def get(self):          

        book_query1 = Book.all()
        shakespeare_books = book_query1.filter("author =", "William Shakespeare")

        template_values = {
            'shakespeare_books': shakespeare_books,             
        }

        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(template_values))


class BookStore(webapp2.RequestHandler):
    def post(self):
        book = Book()
        book.title = self.request.get('title')
        book.author = self.request.get('author')
        book.copyright_year = int(self.request.get('copyright_year'))       
        book.put()  
        self.redirect('/')      


class Book(db.Model):
    title = db.StringProperty()
    author = db.StringProperty()
    copyright_year = db.IntegerProperty()



app = webapp2.WSGIApplication([('/', MainPage),
                                ('/new_book',BookStore)], debug=True)

的index.html

<html>
    <body>                      
        <form action="/new_book" method="post">         
            <label for="title">Title</label>
            <div><textarea name="title" rows="1" cols="20" id="title"></textarea></div>
            <label for="author">Author</label>
            <div><textarea name="author" rows="1" cols="20" id="author"></textarea></div>
            <label for="copyright_year">Copyright Year</label>
            <div><textarea name="copyright_year" rows="1" cols="20" id="copyright_year"></textarea></div>           
            <div><input type="submit" value="Submit"></div>
        </form>

        {% for books in shakespeare_books %}                    
                <b>{{ books.title }}</b> 
                <b>{{ books.author }}</b>
                <b>{{ books.copyright_year }}</b>               
                <hr></hr>           
        {% endfor %}
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

您从请求中获得的字符串为:"1933 1655",而不是您所说的"1933"

您的网页以某种方式发送错误的日期。您的网站上是否有任何验证码,或者根据日期格式化代码?