Django all()得到了一个意想不到的关键字参数'user'

时间:2013-03-09 15:30:31

标签: django

我正在开发一个简单的应用程序。我的应用程序允许用户创建帐户并添加板。

我正在尝试检索用户创建的所有主板并在模板上显示而不会出现此错误。

all() got an unexpected keyword argument 'user'

Traceback:
File "C:\Python26\Lib\site-packages\django\core\handlers\base.py" in get_response
111.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:\o\mysite\pet\views.py" in Display
69.     board = Board.objects.all(user=request.user)

我的models.py

from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.models import User

class Person(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=100)
    image = models.FileField(upload_to="images/",blank=True,null=True)


    def __unicode__(self):
        return self.name

class Board(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

我的views.py部分(问题在这里)

def Display(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('pet.views.LoginRequest'))
    board = Board.objects.all(user=request.user)
    return render(request,'edit.html',{'board':board})

我的edit.html

{% if board %}
<ul>           
    <li>Name: {{ board.name }}</li>

</ul>
{% endif %}

enter image description here

2 个答案:

答案 0 :(得分:3)

all()就是全部。如果要基于值进行查询,请使用filter()

def Display(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('pet.views.LoginRequest'))
    board = Board.objects.filter(user=request.user)
    return render(request,'edit.html',{'board':board})

{% if board %} 
<ul>  
    {% for b in board %}         
    <li>Name: {{ b.name }}</li>
    {% endfor %}
</ul>
{% endif %}

答案 1 :(得分:2)

要了解user request.user的所有内容,您应该使用filter方法。所以相反:

board = Board.objects.filter(user=request.user)

希望这可以解决它!