django 字段 'id' 需要一个数字但得到了 <built-in function id>
django Field 'id' expected a number but got <built-in function id>
我正在学习有关 django todoapp 的教程。我想显示所有用户的 content(他们的 todolists)并且我得到了那个错误看到标题我正在使用 google 的登录授权
这是我的 views.py
def todoView(request,):
all_todo_items = Todoitem.objects.filter(userid=id).values_list('id', flat=True)
return render(request, 'todoapp.html', {'all_items': all_todo_items})
这是我的 models.py
class Todoitem(models.Model):
content = models.CharField(max_length=100)
userid = models.ForeignKey(AuthUser, models.DO_NOTHING, db_column='userid', blank=True,
null=True)
class Meta:
managed = False
db_table = 'todoitem'
def __str__(self):
return self.content
class AuthUser(models.Model):
password = models.CharField(max_length=128)
username = models.CharField(unique=True, max_length=150)
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
email = models.CharField(max_length=254)
class Meta:
managed = False
db_table = 'auth_user'
这是我的 todoapp.html
{% load socialaccount %}
<body>
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }} !</p>
{% else %}
<h1>My Google Login Project</h1>
<a href="{% provider_login_url 'google' %}">Login with Google</a>
{% endif %}
<h2>My Personal TodoApp Project</h2>
<br>
<table>
<tr>
<th colspan="2">List of Todos</th>
</tr>
{% for todo_items in all_items %}
<tr>
<td>{{todo_items.content}}</td>
</tr>
{% endfor %}
</table>
</body>
我希望 id 来自路径。所以,在你的函数中添加 id 参数:
def todoView(request, <b>id=None</b>):
all_todo_items = Todoitem.objects.filter(userid=id).values_list('id', flat=True)
return render(request, 'todoapp.html', {'all_items': all_todo_items})
您将登录用户的 Todoitem
传递给:
from django.contrib.auth.decorators import login_required
@login_required
def todoView(request):
all_todo_items = Todoitem.objects.filter(userid=<strong>request.user</strong>)
return render(request, 'todoapp.html', {'all_items': all_todo_items})
Note: It is normally better to make use of the settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use the User
model [Django-doc] directly. For more information you can see the referencing the User
model section of the documentation.
Note: You can limit views to a view to authenticated users with the
@login_required
decorator [Django-doc].
我正在学习有关 django todoapp 的教程。我想显示所有用户的 content(他们的 todolists)并且我得到了那个错误看到标题我正在使用 google 的登录授权
这是我的 views.py
def todoView(request,):
all_todo_items = Todoitem.objects.filter(userid=id).values_list('id', flat=True)
return render(request, 'todoapp.html', {'all_items': all_todo_items})
这是我的 models.py
class Todoitem(models.Model):
content = models.CharField(max_length=100)
userid = models.ForeignKey(AuthUser, models.DO_NOTHING, db_column='userid', blank=True,
null=True)
class Meta:
managed = False
db_table = 'todoitem'
def __str__(self):
return self.content
class AuthUser(models.Model):
password = models.CharField(max_length=128)
username = models.CharField(unique=True, max_length=150)
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
email = models.CharField(max_length=254)
class Meta:
managed = False
db_table = 'auth_user'
这是我的 todoapp.html
{% load socialaccount %}
<body>
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }} !</p>
{% else %}
<h1>My Google Login Project</h1>
<a href="{% provider_login_url 'google' %}">Login with Google</a>
{% endif %}
<h2>My Personal TodoApp Project</h2>
<br>
<table>
<tr>
<th colspan="2">List of Todos</th>
</tr>
{% for todo_items in all_items %}
<tr>
<td>{{todo_items.content}}</td>
</tr>
{% endfor %}
</table>
</body>
我希望 id 来自路径。所以,在你的函数中添加 id 参数:
def todoView(request, <b>id=None</b>):
all_todo_items = Todoitem.objects.filter(userid=id).values_list('id', flat=True)
return render(request, 'todoapp.html', {'all_items': all_todo_items})
您将登录用户的 Todoitem
传递给:
from django.contrib.auth.decorators import login_required
@login_required
def todoView(request):
all_todo_items = Todoitem.objects.filter(userid=<strong>request.user</strong>)
return render(request, 'todoapp.html', {'all_items': all_todo_items})
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.
Note: You can limit views to a view to authenticated users with the
@login_required
decorator [Django-doc].