python - 如何检索模板中链接到不同对象的对象?姜戈
问题描述
我有一个测验模型和一个视图,其中显示了页面上所有测验的列表。我想显示他们在每个测验旁边得到的分数。目前,我有一个分数模型,它链接到每个测验和参加测验的用户。我想做一些事情,比如Score.objects.filter(user=request.user, quiz=quiz).get_percentage()
显示该测验的百分比。但是,这当然不适用于模板代码。
def learn(request): #Quiz Screen
context = {
'quizzes':Quiz.objects.all(),
'scores':Score.objects.filter(user=request.user),
}
return render(request, 'quiz_app/learn.html', context)
{% extends "quiz_app/base.html" %}
{% block content %}
<a href="{% url 'quiz-create' %}">New Quiz</a>
{% for quiz in quizzes %}
<a id="quiz-anchor" href="{% url 'quiz-detail' quiz.id %}">
<article class="quiz-thumbnail-large">
<h2>{{ quiz.title }}</h2>
<h3>{{ quiz.question_amount }} Questions</h3>
<p>Score: {{ quiz.score.get_percentage }}%</p>
</article>
</a>
{% endfor %}
{% endblock content %}
class Quiz(models.Model):
title = models.CharField(max_length=100) #or TextField
#slug = models.SlugField(max_length=200, default=1) #url
video_link = models.CharField(max_length=100, default="https://www.youtube.com/watch?v=p60rN9JEapg")
question_amount = models.IntegerField()
author = models.ForeignKey(User, default=1, on_delete=models.CASCADE, related_name='quiz_author',)
date_created = models.DateTimeField(default=timezone.now)
class Meta:
verbose_name_plural = "Quizzes"
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('quiz-detail', kwargs={'pk': self.pk})
class Score(models.Model):
quiz = models.ForeignKey(Quiz, default=1, on_delete=models.CASCADE)
user = models.ForeignKey(User, default=1, on_delete=models.CASCADE)
correct_answers = models.IntegerField()
incorrect_answers = models.IntegerField()
def __str__(self): #returns as a/b
score = str(self.correct_answers) + '/' + str(self.get_total_answers())
return score
def add_correct_answers(self, amount):
self.correct_answers = self.correct_answers + amount
self.save()
def add_incorrect_answers(self, amount):
self.incorrect_answers = self.incorrect_answers + amount
self.save()
def get_total_answers(self):
total = self.correct_answers + self.incorrect_answers
return total
def get_percentage(self):
percentage = round(self.correct_answers / self.get_total_answers() * 100)
return percentage
解决方案
因此,由于和之间有外键关系,因此单个测验可以有多个分数,因此您应该使用模型中的反向关系,如下所示:Quiz
Score
quiz
{% extends "quiz_app/base.html" %}
{% block content %}
<a href="{% url 'quiz-create' %}">New Quiz</a>
{% for quiz in quizzes %}
<a id="quiz-anchor" href="{% url 'quiz-detail' quiz.id %}">
<article class="quiz-thumbnail-large">
<h2>{{ quiz.title }}</h2>
<h3>{{ quiz.question_amount }} Questions</h3>
{% for score in quiz.score_set.all %}
<p>Score: {{ score.get_percentage }}%</p>
{% endfor %}
</article>
</a>
{% endfor %}
{% endblock content %}
编辑
这是一个快速修复:
{% extends "quiz_app/base.html" %}
{% block content %}
<a href="{% url 'quiz-create' %}">New Quiz</a>
{% for quiz in quizzes %}
<a id="quiz-anchor" href="{% url 'quiz-detail' quiz.id %}">
<article class="quiz-thumbnail-large">
<h2>{{ quiz.title }}</h2>
<h3>{{ quiz.question_amount }} Questions</h3>
{% for score in quiz.score_set.all %}
{% if score.user == request.user %}
<p>Score: {{ score.get_percentage }}%</p>
{% endif %}
{% endfor %}
</article>
</a>
{% endfor %}
{% endblock content %}
但这不是一个好主意,因为您将首先检索所有用户的所有分数,然后对其进行过滤;这里最好使用模板标签。
推荐阅读
- node.js - 为什么 sinon 会忽略我对另一个函数的调用?
- php - 在 Macroable.php 第 104 行:Method Illuminate\Database\Schema\Blueprint::id 不存在
- aws-lambda - 用于生成 AWS lambda 包的 Bazel 规则
- python - 是否可以尝试多个 XPATH?蟒蛇硒
- json - Cypress:读取,修改和写入以变量作为字段名称的json文件
- python - 在python中突出显示更改的单词
- testrigor - TestRigor - 如何按活动测试过滤?还有其他方法可以启用/禁用测试吗?
- php - 查询两个表 ORDER BY 日期
- ios - Flutter 2 升级:CocoaPods 未安装或无效状态
- jmeter - Jmeter --我无法理解为什么我的 Jmeter 脚本没有为我的应用程序使用动态 session_code ...任何帮助将不胜感激