Mysql 尝试获取特定数据的问题数量(计数),并使用此信息验证Django模板中的输入字段值
在我的项目中,我有一个表,其中包含特定主题和子主题的问题。我想计算特定主题id的每个子主题的问题数量。这里我有一个名为子主题的查询,其中包含关于特定主题的子主题的信息,另一个是名为total的列表,其中包含名为子主题的查询中存在的子主题的所有问题 在models.py问题表中Mysql 尝试获取特定数据的问题数量(计数),并使用此信息验证Django模板中的输入字段值,mysql,django,django-views,django-templates,django-orm,Mysql,Django,Django Views,Django Templates,Django Orm,在我的项目中,我有一个表,其中包含特定主题和子主题的问题。我想计算特定主题id的每个子主题的问题数量。这里我有一个名为子主题的查询,其中包含关于特定主题的子主题的信息,另一个是名为total的列表,其中包含名为子主题的查询中存在的子主题的所有问题 在models.py问题表中 class Questions(models.Model): grade = models.ForeignKey(Add_Grade,on_delete=models.CASCADE, default = 1)
class Questions(models.Model):
grade = models.ForeignKey(Add_Grade,on_delete=models.CASCADE, default = 1)
topic = models.ForeignKey(Add_Topics, on_delete= models.CASCADE , default = 1)
sub_topic = models.ForeignKey(Sub_Topics, on_delete= models.CASCADE, default = 1)
question = models.CharField(max_length=100)
image= models.URLField(null=True)
difficulty=models.CharField(max_length=100, default='')
class Meta:
db_table = 'questions'
in views.py
def showassess(request):
if request.method=='POST':
classname=request.POST.get("classname")
topicid=request.POST.get("topicid")
topicname=request.POST.get("topicname")
print(topicname,topicid)
gradeid=request.POST.get("gradeid")
subtopics=Sub_Topics.objects.filter(topic__id=topicid)
print(subtopics)
listt=list(Sub_Topics.objects.values_list('id',flat=True).filter(topic__id=topicid))
q=0
totall=[]
for p in listt:
if q <= len(listt):
totalques=Questions.objects.filter(sub_topic__id=listt[q]).count()
totall.append(totalques)
q=q+1
#totalques=Questions.objects.filter(sub_topic__in=listt)
print(listt)
print(totall)#list contain no of questions of each subtopic having specic topic_id
context={
'totall':totall,
'topicid':topicid,
'topicname':topicname,
'classname':classname,
'gradeid':gradeid,
'subtopics':subtopics,
}
return render(request,"assessment.html",context)
def显示评估(请求):
if request.method==“POST”:
classname=request.POST.get(“classname”)
topicid=request.POST.get(“topicid”)
topicname=request.POST.get(“topicname”)
打印(主题名称、主题ID)
gradeid=request.POST.get(“gradeid”)
subtopics=Sub\u Topics.objects.filter(topic\u id=topicid)
打印(副标题)
listt=list(Sub\u Topics.objects.values\u list('id',flat=True).filter(topic\u id=topicid))
q=0
总计=[]
对于列表中的p:
if q列表(包含子主题id)
[3,2,1,1]->total(上述子主题的问题总数[例如:id=1的子主题
共3个问题,id=2的子主题有2个问题]
在模板中,我只想根据上面在views.py中评估的问题数量,验证用户在每个子主题的数字输入字段中输入的问题数量。目的是限制用户,以便他可以输入正确的问题数量。
在views.py的上下文中,我有一个名为subtopics的查询,以及一个包含查询中每个子主题的全部问题的列表。我如何使用这些数据来获得所需的结果,或者是否有更好的方法来获得所需的场景
<div class="card text-center">
<div class="card-header">
Subtopics of {{topicname}}
</div>
<div class="card-body">
<form id="assessment" action="selectassess" method="POST">
{% csrf_token %}
{% for t in subtopics %}
<input type="hidden" name="classname" value="{{classname}}">
<input type="hidden" name="gradeid" value="{{gradeid}}">
<input type="hidden" name="topicname" value="{{topicname}}">
<input type="hidden" name="topicid" value="{{topicid}}">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="assess" value="{{t.id}}" >
<label class="form-check-label" for="assess">{{t.subtopic}} {{p}}</label>
</div>
<input type="number" placeholder="No of ques to be selected" min="0" max="10" name="quesno" width="60"><br>
{% endfor %}
</form>
</div>
<div class="card-footer">
<input class="btn btn-primary" form="assessment" type="submit" value="Submit">
</div>
</div>
{{topicname}}的子主题
{%csrf_令牌%}
{子主题中t的百分比%}
{{t.次主题}{{p}
{%endfor%}