[英]Django view only returning primary key from field to template, no data
I have a model which has a column categories in a Product table, which has one entry, 'beds'. 我有一个模型,该模型在“产品”表中有一个列类别,该表中有一个条目“床”。
When I try to get a list of categories, or products (all of which have names, descriptions, etc), the only thing I can get returned is numbers. 当我尝试获取类别或产品的列表(所有这些都具有名称,描述等)时,我唯一能得到的就是数字。
This is the relevant view from my views.py 这是我的views.py中的相关视图
def product(request):
template = loader.get_template('/home/user/webapps/webapp/my_site/main_page/templates/main_page/product.html')
prods = xroduct.objects.values_list('product')
context={'prods': prods}
return HttpResponse(template.render(context))
This is my code within a django template to display data 这是我在Django模板中用于显示数据的代码
{% for instance in prods %}
<li>{{ instance }}</li>
{% endfor %}
However, all that gets returned, going by HTML when viewing the webpage, is: 但是,返回的所有内容(在查看网页时使用HTML)是:
<li>(2,)</li>
<li>(1,)</li>
There should be a lot more information returned. 返回的信息应该很多。 Names, descriptions, etc. Why is this not being returned via my view? 名称,说明等。为什么没有通过我的视图返回此信息?
edit: How xroduct is defined: 编辑:如何定义xroduct:
from oscar.core.loading import get_class, get_model
xroduct = get_model('catalogue', 'product')
Since we are talking about django-oscar
there are number of things to understand: 由于我们在谈论django-oscar
因此有很多事情要理解:
To retrieve the models from oscar you need to use get_model
which is their own implementation of dynamically importing the models of interest. 要从oscar检索模型,您需要使用get_model
,这是他们自己的动态导入感兴趣模型的实现。 get_model
fetched the models from https://github.com/django-oscar/django-oscar/blob/master/src/oscar/apps/catalogue/models.py whic are defined by https://github.com/django-oscar/django-oscar/blob/master/src/oscar/apps/catalogue/abstract_models.py get_model
从https://github.com/django-oscar/django-oscar/blob/master/src/oscar/apps/catalogue/models.py获取了模型,而这些模型是由https://github.com/django-定义的奥斯卡/ Django的奥斯卡/斑点/主/ SRC /奥斯卡/应用/目录/ abstract_models.py
What you need to do if you want to list products and their information is the following: 如果要列出产品及其信息,需要执行以下操作:
from oscar.core.loading import get_model
Product = get_model("catalogue", "Product")
def list_products(request):
template = loader.get_template(...)
products = Product.objects.all()
context = {"products": products}
return HttpResponse(template.render(context))
And in the template you can simply access the instances like: 在模板中,您可以像下面这样简单地访问实例:
{% for instance in prods %}
<li>{{ instance.title }}</li>
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.