Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 赢得的项目';t显示_Python_Django - Fatal编程技术网

Python 赢得的项目';t显示

Python 赢得的项目';t显示,python,django,Python,Django,url.py item.html from django.http import HttpResponse from item.models import ItemCategory, Item from django.shortcuts import render_to_response, get_object_or_404 def item(request, cat_id): item_list = get_object_or_404(ItemCategory, pk=cat_id)

url.py

item.html

from django.http import HttpResponse
from item.models import ItemCategory, Item
from django.shortcuts import render_to_response, get_object_or_404

def item(request, cat_id):
    item_list = get_object_or_404(ItemCategory, pk=cat_id)
    return render_to_response('order/item.html', {'item_list':item_list})
但它只是这样显示:

第一类 它不显示项目


这方面可能有什么问题

您是否从教程中复制了此代码?连接的模型是否被故意称为
choice\u set
?如果您的
项目
型号具有
外键(项目类别)
,则它应该是
项目集
。但这只是一个纯粹的猜测

在返回之前,我会在视图中放置一个调试行,并检查该项在选项集中是否有这些选项。如果您有一个调试器,并且可以看到真正发生的事情,而不是阅读代码和猜测,那么问题就需要解决很多

- item 1
- item 2
或者更好的IPDB(您必须使用easy_install或pip进行安装)

在调试器中,编写变量并检查它是否符合预期:

def item(request, cat_id):
    item_list = get_object_or_404(ItemCategory, pk=cat_id)
    import ipdb; ipdb.set_trace()
    return render_to_response('order/item.html', {'item_list':item_list})
(Offtopic)有一个django_恼人的应用程序,带有有用的快捷方式:

>>> item_list
[some output]
>>> item_list.choice_set.all()
[some output]

您忘记在问题中添加您的
models.py
def item(request, cat_id):
    item_list = get_object_or_404(ItemCategory, pk=cat_id)
    import pdb; pdb.set_trace()
    return render_to_response('order/item.html', {'item_list':item_list})
def item(request, cat_id):
    item_list = get_object_or_404(ItemCategory, pk=cat_id)
    import ipdb; ipdb.set_trace()
    return render_to_response('order/item.html', {'item_list':item_list})
>>> item_list
[some output]
>>> item_list.choice_set.all()
[some output]
from annoying.decorators import render_to

@render_to('order/item.html')
def item(request, cat_id):
    item_list = get_object_or_404(ItemCategory, pk=cat_id)
    return {'item_list':item_list}