Django:以10为底的int()的无效文字 [英] Django: invalid literal for int() with base 10

查看:150
本文介绍了Django:以10为底的int()的无效文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的新手,他试图将作者的名字传递到视图中,并根据作者的名字过滤掉引用对象。以下是代码:

I am new to Django and trying to pass an author's name to a view and filter out quote objects based on the author's name. here are the codes:

models.py

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/
(您是已经创建的测试作者对象)

However I get this error when I try to get http://127.0.0.1:8000/quotes/you/ ('you' is a test author object, already created)

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']

感谢您为解决此问题所提供的帮助。

I appreciate your help to resolve this.

推荐答案

您要搜索作者的 author_name 字段,而不是

You want to search on the author's author_name field, not the id.

Quote.objects.filter(author__author_name=name)

在当前搜索中, author__exact ,Django期望 name 是作者的ID,因此会出现错误,因为 you 不是整数。

With your current search, author__exact, Django expects name to be the id of the author, so gives an error because you is not an integer.

这篇关于Django:以10为底的int()的无效文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆