python - 如何根据两个表之间的共同属性值显示一个表的数据?
问题描述
我正在尝试根据表'Student_class'的公共属性值显示表'Class'中的所有数据,其中(Student_class.course_id==Class.id)。有一个错误。我该如何解决这个问题?
模型.py
class Class(models.Model):
course_code = models.CharField(max_length=111, default="")
course_title = models.CharField(max_length=111, default="")
course_credit = models.FloatField(default=0.0)
section = models.CharField(max_length=111, default="")
teacher = models.ForeignKey(User, on_delete=models.CASCADE)
code = models.CharField(max_length=10, default="", unique=True)
def __str__(self):
return self.course_title
class Student_class(models.Model):
student = models.ForeignKey(User, on_delete=models.CASCADE)
marks = models.FloatField(default=0.0)
answered_ques = models.IntegerField(default=0)
join_date = models.DateTimeField(default=datetime.now, blank=True)
course_id = models.ForeignKey(Class, on_delete=models.CASCADE)
视图.py
@login_required
def my_class(request):
current_user = request.user
joined_class = Student_class.objects.filter(student=current_user).order_by('-id')
all_class = Class.objects.filter()
created_class = Class.objects.filter(teacher=current_user).order_by('-id')
image = Profile.objects.get(user_id = current_user)
params = {'created_class': created_class, 'teacher_name': current_user, 'image': image, 'all_class': all_class, 'joined_class': joined_class }
return render(request, 'course/my_class.html', params)
my_class.html
{% for j in created_class%}
{% for i in all_class%}
{% if j.course_id==i.id %}
<div class="service text-center">
<a href="#"><img src='{{image.profile_image}}' alt="Image" onerror="this.src='../../static/course/images/default-avatar.jpg'"
style="border-radius: 50%;width: 80px;height: 80px; margin-left: 130px;"class="img-fluid"></a>
<div class="px-md-3">
<h3><a href="#">{{i.course_code }} {{i.course_title }}</a></h3>
<p>Course credit: {{i.course_credit}}<br> {{i.section}}</p>
</div>
</div>
{% endif %}
{% endfor %}
{% endfor %}
堆栈跟踪
TemplateSyntaxError at /my_class/
Could not parse the remainder: '==i.id' from 'j.course_id==i.id'
Request Method: GET
Request URL: http://127.0.0.1:8000/my_class/
Django Version: 2.1.5
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: '==i.id' from 'j.course_id==i.id'
Exception Location: C:\Users\hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.5-py3.7.egg\django\template\base.py in __init__, line 663
Python Executable: C:\Users\hp\AppData\Local\Programs\Python\Python37\python.exe
Python Version: 3.7.6
Python Path: [
'D:\\django\\Course-Management-G35',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python37\\lib',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python37',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\django-2.1.5-py3.7.egg',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\pytz-2018.9-py3.7.egg',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\win32',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\win32\\lib',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\Pythonwin'
]
Server time: Sat, 2 Jan 2021 16:11:27 +0000
解决方案
更改模板中的条件
从:
{% if j.course_id==i.id %}
{% endif %}
至:
{% ifequal j.course_id i.id %}
{% endifequal %}
推荐阅读
- wordpress - Wordpress - 自定义帖子类型 - 并非所有属性都被设置
- python - MultipleLocator 为散点图生成非整数刻度
- c++ - 如何编写在 C++ 中进行实际记录的可变参数记录器函数
- c - memcpy 和内存位置
- file - 使用 perl 在没有子文件夹的文件夹中搜索特定文件
- r - 在 ubuntu 中更新 R 后遇到问题
- c# - 将应用程序发送到后台时,Xamarin.iOS 应用程序中的 HTTP 调用崩溃
- python - 子类化numpy:numpy方法返回ndarray而不是sublassed类型?
- php - PHP PDO Bind Param with having Clause,其中聚合函数与变量混合
- arrays - 如何通过两个键对多维数组进行排序