我是Django的新手,尝试将作者姓名传递给视图,并根据作者姓名筛选出引用对象。这里是代码:Django:int()的基数为10的无效字面积
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']
我感谢您的帮助来解决这个问题。
这是一个粘贴错误,你的.html模板没有引号吗? – jdi 2011-12-17 21:41:32
也请发布更完整的回溯,因为它应该告诉你错误的特定行和上下文。尝试将名称设置为缺省关键字,例如quotesbyauthor(request,name ='') – jdi 2011-12-17 21:45:58
@jdi,我用更详细的错误消息更新了问题。另外,我在模板名称周围添加了引号,但它没有效果。 – qliq 2011-12-17 21:51:47