在Django中使用查询的问题 [英] Issue with using query in Django
问题描述
所以我想要得到的是以下内容.用户可以选择代码中看到的3个类别.
So what I'm trying to get is the following. User can choose 3 categories as seen in the code.
在这些类别中,他们可以添加任意数量的植物.在我看来:集合我希望用户看到以下内容:
Inside those categories they can add any number of plants. In my view: collection I want the user to see the following:
收藏
用户选择的类别
- plant_1
- plant_2
- ....
Category_selected_by_user2
Category_selected_by_user2
我现在得到的是:
收藏
用户选择的类别
Category_selected_by_user2
Category_selected_by_user2
Category_selected_by_user3
Category_selected_by_user3
所以基本上这是models.py中的代码:
so basically this is the code in models.py:
class Plant_category(models.Model):
"""This class contains the users different categories"""
CATEGORY_CHOICES = [
('Houseplant', 'Houseplant'),
('Succulents', 'Succulents'),
('Outdoor', 'Outdoor'),
]
"""Plural for multiple categories"""
class Meta:
verbose_name_plural = 'Categories'
"""Returns the above stated choices"""
category = models.CharField(max_length=50, choices=CATEGORY_CHOICES)
def __str__(self):
return self.category
class Plant_name(models.Model):
"""This class contains the plant name that is housed within a certain category"""
"""Links the plant to one of the chosen categories"""
category = models.ForeignKey(Plant_category, on_delete=models.CASCADE, related_name='plant_names')
# Placeholder for connection with a plant database API
plant = models.CharField(max_length=50)
"""Return the plant input from the user"""
def __str__(self):
return self.plant
这是views.py中的代码:
this is the code in views.py:
def collection(request):
"""The page that opens the collection of plants"""
plant_categories = Plant_category.objects.all().order_by('category')
context = {
'plant_categories': plant_categories,
}
return render(request, 'plntz_main/collection.html', context)
这是collection.html中的代码:
And this is the code in collection.html:
<ul>
{% for category in plant_categories %}
<h3>{{ category }}</h3>
{% for name in plant_categories.plant_names.all %}
<li>{{ name.plant }}</li>
{% endfor %}
{% empty %}
<li>No category has been added yet.</li>
{% endfor %}
</ul>
类别中工厂的提取数据一定有问题.我似乎找不到导致这种情况的原因.
There must be something wrong with the data pulled for the plants inside the category. I can't seem to find what is causing this.
例如,这是在室内植物"下添加的管理数据:
For example this is the admin data that has been added under Houseplant:
- houseplant_1
- houseplant_2
这是在多肉植物下添加的:
And this was added under Succulents:
- 仙人掌_1
- 仙人掌_2
推荐答案
这可能会导致问题:您没有正确地引用对象/查询集,因此不会在前端明智地呈现任何数据.
Imho this causes the problem: You don't refer to the object/queryset correctly, so no data is being rendered frontend wise.
您的上下文包含对象 plant_categories
,该对象应相应地循环:
Your context contains the object plant_categories
which should be looped accordingly:
<ul>
{% for category in plant_categories %}
<h3>{{ category }}</h3>
# Refer to object within context
{% for name in plant_categories %}
<li>{{ name.plant }}</li> # Depending on what you would like to render you have to adapt here
{% endfor %}
{% empty %}
<li>No category has been added yet.</li>
{% endfor %}
您可以在视图中添加打印语句以检查查询集:
You can add a print statement to your view to check the queryset:
def collection(request):
"""The page that opens the collection of plants"""
plant_categories = Plant_category.objects.all().order_by('category')
context = {
'plant_categories': plant_categories,
}
# Print for debugging
print(plant_categories)
return render(request, 'plntz_main/collection.html', context)
这篇关于在Django中使用查询的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!