内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/8548023/django-invalid-literal-for-int-with-base-10
我是Django的新手,试图将作者的名字传递给一个视图,并根据作者的姓名筛选出报价对象.以下是代码:
models.py
class Author(models.Model): author_name = models.CharField(max_length=50, default='unknown') author_info = models.TextField(max_length=1000) class Quote(models.Model): author = models.ForeignKey(Author) quote = models.TextField(max_length=500) category= models.ForeignKey(Category) pub_date = models.DateTimeField('date published')
urls.py:
url(r'^quotes/(?P<name>\w+)/$', 'quotes.views.quotesbyauthor'),
views.py
def quotesbyauthor(request, name): aquotelist = Quote.objects.filter(author__exact = name) return render_to_response(quotes_by_author.html, {'aquotelist': aquotelist })
但是当我尝试获得 http://127.0.0.1:8000/quotes/you/ 时,我收到这个错误
(‘你’是一个测试作者对象,已经创建)
ValueError at /quotes/you/ invalid literal for int() with base 10: 'you' Request Method: GET Request URL: http://127.0.0.1:8000/quotes/you/ Django Version: 1.3.1 Exception Type: ValueError Exception Value: invalid literal for int() with base 10: 'you' Exception Location: /home/qliq/djenv/lib/python2.6/site-packages/django/db/models/fields/__init__.py in get_prep_value, line 479 Python Executable: /home/qliq/djenv/bin/python Python Version: 2.6.6 Python Path: ['/home/qliq/djenv/quoteapp', '/home/qliq/djenv/lib/python2.6/site-packages/distribute-0.6.10-py2.6.egg', '/home/qliq/djenv/lib/python2.6/site-packages/pip-0.7.2-py2.6.egg', '/home/qliq/djenv/lib/python2.6', '/home/qliq/djenv/lib/python2.6/plat-linux2', '/home/qliq/djenv/lib/python2.6/lib-tk', '/home/qliq/djenv/lib/python2.6/lib-old', '/home/qliq/djenv/lib/python2.6/lib-dynload', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/home/qliq/djenv/lib/python2.6/site-packages']
感谢您的帮助解决这个问题.
你想搜索作者的author_name字段,而不是id.
Quote.objects.filter(author__author_name=name)
使用你当前的搜索,author_exact,Django希望name作为作者的id,所以给出一个错误,因为你不是一个整数.
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/8548023/django-invalid-literal-for-int-with-base-10
以上所述就是小编给大家介绍的《Django:int()与base 10无效的文字》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。