-2

当我在表单中输入 1933 作为 copyright_year 时出现以下错误:

文件“C:\programming_google_app_engine_repo\datastore\main.py”,第 29 行,post book.copyright_year = int(self.request.get('copyright_year')) ValueError: int() 以 10 为底的无效文字:'1933 1655'

为什么我会收到此错误?

主文件

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)

索引.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>
4

1 回答 1

1

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

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

于 2013-03-21T22:52:09.550 回答