【问题标题】:Django “TypeError: list() got an unexpected keyword argument 'id''” errorDjango “TypeError: list() got an unexpected keyword argument 'id''” 错误
【发布时间】:2016-12-02 14:29:35
【问题描述】:

我正在尝试重定向到一个页面,我打算在创建一个对象后将其实现为一个对象的主页。 视图.py

from django.shortcuts import render, get_object_or_404
from f.models import Post


def list(request):
    post = Post.objects.all()
    context = {
        'post': post,
    }
    return render(request, 'list.html', context)


def detail(request, id=None):
    Post = get_object_or_404(post, id=id)
    context = {
        'Post': Post,
    }
    return render(request, 'detail.html', context)

url.py

urlpatterns = [
    url(r'^$', views.list, name='list'),
    url(r'^(?P<id>[0-9]{1,3})$', views.list, name='detail'),
]

还有我的错误

Django Version:     1.9.10
Exception Type:     TypeError
Exception Value:    

list() got an unexpected keyword argument 'id'

Python Version:     3.5.2

【问题讨论】:

  • 完整的堆栈跟踪在解决问题方面大有帮助
  • 在您的 urlpatterns 中,第二项应该包含 views.detail,而不是 views.list

标签: python django


【解决方案1】:

看路线:

url(r'^(?P<id>[0-9]{1,3})$', views.list, name='detail'),

您将详细 url 发送到列表视图 views.list 而不是详细视图 views.detail

顺便说一句,最好为列表视图选择一个不同的名称,因为list 隐藏了内置名称list

【讨论】:

  • 谢谢你,我有新问题 NameError: name 'post' is not defined views.py def detail(request, id=None): posts1 = get_object_or_404(post, id=id) context = { 'posts1': posts1, } return render(request, 'detail.html', context) def list(request): post = Post.objects.all() context = { 'post': post, } return render(request, 'list.html',上下文)
  • 应该是post = get_object_or_404(Post, id=id),而不是Post = get_object_or_404(post, id=id)
  • tnx 但未在页面def detail(request, id=None): post = get_object_or_404(Post, id=id) context = { 'post': post, } return render(request, 'detail.html', context) 中显示任何详细信息
猜你喜欢
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
  • 2015-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 2015-09-23
相关资源
最近更新 更多